Extracting data from JSON file shows /Date(1435837792000+0000)/
How can I format the date to appear as Oct 29, 2010 9:10:23 AM
?
Extracting data from JSON file shows /Date(1435837792000+0000)/
How can I format the date to appear as Oct 29, 2010 9:10:23 AM
?
In Javascript, the Date prototype does not have a function like format(). However, there are several methods that can be used:
getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year
getHours() // Returns the hour
getMinutes() // Returns the minute
getSeconds() // Returns the second
Instead of a format function, you can create your own formatting function using the following code snippet:
function formatDate(dt) {
var monthNames = ["January", "February", "March", ...];
var h = dt.getHours(), am;
if (h > 12) {
am = 'pm';
h = h - 12;
} else {
am = 'am';
}
return monthNames[dt.getMonth()].substring(0, 3) + ' ' +
dt.getDate() + ', ' + dt.getFullYear() + ' ' +
h + ':' + dt.getMinutes() + ':' + dt.getSeconds() + ' ' +
am;
}
console.log(formatDate(new Date(1435837792000+0000)));
If you require more advanced date parsing and formatting capabilities, consider exploring the Momentjs library. Using Moment.js, you can easily achieve formatted dates with just one line of code:
moment(new Date(1435837792000+0000)).format("MMM D, YYYY hh:mm:ss A")
Utilize JavaScript to convert the data into a Date object and then perform date functions on it.
<html>
<head>
<script>
function jsonDatetoJSDate(){
var dateFromServer = "/Date(1435837792000+0000)/";
//JavaScript conversion
var parsedDate = new Date(parseInt(dateFromServer.substr(6)));
//obtaining Date Object
var updatedDate = new Date(parsedDate);
alert(updatedDate);
}
</script>
</head>
<body onload="javascript:jsonDatetoJSDate()">
Perform Date Functions using JavaScript
</body>
</html>
When it comes to the JSON data you receive, it may look something like this based on your comments:
var obj = {
"PublishDate": "\/Date(1435757849000+0000)\/"
}
If you are unable to change the value due to being surrounded by Date() and slashes, you can try the following steps:
First, escape the slashes around Date
.
obj.PublishDate.replace(/\//g, "");
Then, evaluate the remaining expression.
eval(obj.PublishDate.replace(/\//g, ""))
This should give you the actual date: Wed Jul 08 2015 11:48
To retrieve this value from your JSON data, follow these steps:
Firstly, assign it to a scope variable within your controller file (yourCtrl.js).
json = { name: 'stackoverflow',
date: 1435837792000+0000
};
$scope.today = new Date(json.date); //This will convert the date value from the JSON to a more readable format like `2015-07-02T11:49:52.000Z`
In your HTML file (today.html), use the following code snippet to display the date in your desired format:
{{ today | date:'medium' }}
The angular filter provided above will help you display the date as 'Oct 29, 2010 9:10:23 AM'.
If you need to apply additional formatting, you can do so by updating the code in this way:
$scope.today = Date(1435837792000+0000);
$scope.today = new Date($scope.today);
Next, utilize the angular filters for further customization:
{{ today | date:'MMM d, y hh:mm:ss' }}
or {{ today | date:'medium' }}
based on your specific requirements.
Start by extracting the timestamp from JSON and storing it in a Date
variable using the following method:
var json = { "PublishDate": "\/Date(1435757849000+0000)\/" };
var timestamp = parseInt(json.PublishDate.match(/\d+/)[0],10);
var date = new Date(timestamp);
Next, you can combine .toDateString()
and .toLocaleTimeString()
, which may not be completely reliable but will provide localized results:
date = date.toDateString() + ' ' + date.toLocaleTimeString('en');
alert( date === 'Wed Jul 01 2015 3:37:29 PM' );
Alternatively, you can use .toGMTString()
to get a result like Thu, 02 Jul 2015 11:49:52 GMT
(following RFC 1123).
Encountered this issue while attempting to integrate ky into a Next.js project: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /foo/node_modules/ky/index.js It seems that the cause of this problem is Webpack (or Babel) converting all import ...
Although it should be as simple as breathing, I just can't seem to spot the mistake in my code... I attempted to add a click event to a div with a unique ID, but unfortunately, it's not working at all. This issue persists with both jQuery and Ja ...
Within my application, I have various classes dedicated to generating XML strings. Each of these classes contains specific methods that take input arguments and produce a string output. In order to enforce this structure and prevent the addition of methods ...
I am trying to figure out how to effectively pass the variable 'country' from the Layout component to its children without using state management. Basically, I want to drill it down. import { useState, useEffect } from 'react' import La ...
I developed a unique operator that utilizes the this keyword, but I am encountering an issue where it always returns undefined. Even though I used bind to pass this into the function. My special operator function shouldLoadNewOptimizationData() { retu ...
I am working with a JSON file that has one of its attributes stored as an array. My goal is to continuously add new values to this array and save it back to the file. Is there a method to avoid overwriting existing data and simply append the new values ins ...
Is there a way to hide certain elements in the index page based on the URL of nested views? In my index.html file, I am looking to implement something like this: <top-bar ng-if="!home"></top-bar> <ui-view class="reveal-animation"> ...
I have been facing an issue with calling a webhook from Dialogflow. I am not receiving any response from the webhook, and instead, I am getting the response from the intent section that I have configured. I have made sure to enable the webhook for each int ...
Is it possible to apply a loader image to a specific section of content on a webpage? All the tutorials I've come across for loader images focus on applying them to entire webpages. However, I am looking to implement a simple loader only to a specifi ...
Trying to retrieve the height of a series of blocks using Puppeteer, I encountered an issue where I couldn't select my block in page.evaluate() due to an error. The code snippet in question is as follows: (async () => { const browser = aw ...
In my code, I need the following conditions to be met: when an image is posted from the backend, the video and audio sources should automatically hide; when a video is posted, the image and audio sources should hide; and when an audio file is posted, the v ...
Currently, I am going through the tutorials and documentation for Angularjs that is provided on the official Angularjs website. One of the examples involves adding a select box for ordering which looks like this: <select ng-model="orderProp"> ...
Exploring the world of Blazor web assembly, I embarked on a project to harness the power of JSInterop with Three.JS to draw lines. Following the guidelines provided in their tutorials available Here, I diligently installed Three.JS using npm and webpack, w ...
Is it possible to modify my jQuery Google Instant style search script to pull content from a PHP script instead of using the BingAPI? Below is the current code being used: $(document).ready(function(){ $("#search").keyup(function(){ var searc ...
Great news - my lightbox is up and running! Each image on my website corresponds to a different organization, and clicking on the image opens a specific lightbox. My question is: how can I have a lightbox configured so that only the trigger image is displ ...
Is there a method to manipulate the data transmitted by socket.io just before it is sent or received? I was considering implementing something like an express middleware. This way, I could encode the data after the normal .emit() call and before the socket ...
I'm encountering issues with an Ajax login function. Despite finding a similar question that didn't help, I'm still unsure of the problem. It's perplexing because this code functions without any problems in another project. Hopefully, ...
Struggling to insert something in my database and then access it directly after. Due to the asynchronous nature of node.js, my plan doesn't seem to work out. However, I am confident that there is a solution to make this happen. Here's the code sn ...
Currently, I am in the process of developing a NextJS application and facing a challenge with accessing a cookie to utilize it for setting a Http Header within a GraphQL Request. For this task, I am integrating apollo-link-context. Below is the snippet of ...
I have successfully created a basic angular5 component called HelloComponent: var HelloComponent = function () { }; HelloComponent.annotations = [ new ng.core.Component({ selector: 'hello-world', template: 'Hello World!' } ...