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")
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
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.
I want to split the following string into 2 arrays. First array from the beginning to '/' and the second from '/' to end. Also, I've tryied with replace to remove the '/' but returns error. var str ...
I want to split the following string into 2 arrays. First array from the beginning to '/' and the second from '/' to end. Also, I've tryied with replace to remove the '/' but returns error. var str ...
I am storing an array of objects in localStorage, retriving it each time, attachibg the new object and again storing. the code works fine, only if there is already an entry in localStorage Following ...
I am storing an array of objects in localStorage, retriving it each time, attachibg the new object and again storing. the code works fine, only if there is already an entry in localStorage Following ...
So I have an HTML table populated with negative and positive values. I want to query the specific values and then upon hovering over the table values less than 0 I want to highlight the row with a red ...
So I have an HTML table populated with negative and positive values. I want to query the specific values and then upon hovering over the table values less than 0 I want to highlight the row with a red ...
url for LinkedIn advanced search: https://www.linkedin.com/vsearch/p?adv=true&trk=advsrch I am trying to fill fields and hit the submit button on the LinkedIn advanced search page using Selenium (...
url for LinkedIn advanced search: https://www.linkedin.com/vsearch/p?adv=true&trk=advsrch I am trying to fill fields and hit the submit button on the LinkedIn advanced search page using Selenium (...