Here is what i'm getting from json file /Date(1435837792000+0000)/
I need to display date in the following format Oct 29, 2010 9:10:23 AM
Here is what i'm getting from json file /Date(1435837792000+0000)/
I need to display date in the following format Oct 29, 2010 9:10:23 AM
There is no function like format() on the Date prototype in Javascript. But there are these methods:
getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year
getHours() // Returns the hour
getMinutes() // Returns the minute
getSeconds() // Returns the second
You can form the string yourself using:
function formatDate(dt) {
var monthNames = ["January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"];
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 are finding yourselves in need of more date parsing and formatting, check out the Momentjs library. With moment, you could just do:
moment(new Date(1435837792000+0000)).format("MMM D, YYYY hh:mm:ss A")
You can extract this value from your json.
Assign it to a scope variable in the controller. yourCtrl.js
json = { name: 'stackoverflow',
date: 1435837792000+0000
};
$scope.today = new Date(json.date); //This converts the date value from the json to a healthy looking date something like this `2015-07-02T11:49:52.000Z`
today.html
{{ today | date:'medium' }}
This angular filter will display your date in the desired date format. Oct 29, 2010 9:10:23 AM
EDIT:
$scope.today = Date(1435837792000+0000);
$scope.today = new Date($scope.today);
and then pipe it up as using the angular filters as follows
{{ today | date:'MMM d, y hh:mm:ss' }}
or {{ today | date:'medium' }}
based upon your requirement.
First get the timestamp from JSON to a Date
variable, here’s a brute match
:
var json = { "PublishDate": "\/Date(1435757849000+0000)\/" };
var timestamp = parseInt(json.PublishDate.match(/\d+/)[0],10);
var date = new Date(timestamp);
Then, a mix of .toDateString()
and .toLocaleTimeString()
, both quirky (yet localized) and possibly unreliable non-standard, might come close:
date = date.toDateString() + ' ' + date.toLocaleTimeString('en');
alert( date === 'Wed Jul 01 2015 3:37:29 PM' );
There’s also .toGMTString()
that returns Thu, 02 Jul 2015 11:49:52 GMT
(RFC 1123) respectively.
Use JS to convert it to Date object & then date function on it..
<html>
<head>
<script>
function jsonDatetoJSDate(){
var dateFromServer = "/Date(1435837792000+0000)/";
//Javascript conversion
var prsdDt = new Date(parseInt(dateFromServer.substr(6)));
//getting Date Object
var uDate = new Date(prsdDt);
alert(uDate);
}
</script>
</head>
<body onload="javascript:jsonDatetoJSDate()">
Use Date Function on javaScript
</body>
</html>
As you mentioned in comments, if the JSON you are getting is something like
var obj = {
"PublishDate": "\/Date(1435757849000+0000)\/"
}
and you have no control in changing the value(since you are enclosing the Date() around slashes), then you would need to
escape the slashs around Date
.
obj.PublishDate.replace(/\//g, "");
and evaluate the remaining expression
eval(obj.PublishDate.replace(/\//g, ""))
to get the actual Date: Wed Jul 08 2015 11:48