Extract Month or Day or Year from Timestamp
19,913
Solution 1
var d = new Date(1397639141184);
alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear());
Solution 2
1.If it's JavaScript Timestamp(i.e., in milliseconds)
var date = new Date(13976391000);
var date = date.getDate(); //returns date (1 to 31) you can getUTCDate() for UTC date
var day = date.getMonth(); // returns 1 less than month count since it starts from 0
var year = date.getFullYear(); //returns year
// You can also use getHours(), getMinutes() and so on
2.If it's database timestamp - example 2013-03-14T02:15:00
var date = new Date('2013-03-14T02:15:00'); // Works in all browsers
var date = new Date('2013-10-18 08:53:14');// works in Chrome & doesn't work in IE/Mozilla
//note that its a string and you use the same above functions to get Date,month & year
Author by
TeO
Updated on June 28, 2022Comments
-
TeO almost 2 years
I would like to know how to get a specific date format (date or month or day or year) from a timestamp. I am wanting to use this in a view with Backbone JS
-
TeO about 10 yearsThank you very much for your help. You are the best !
-
hidden_4003 about 10 yearsRegarding 2nd sample, MDN states: The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). Your shown format is neither. Has to be new Date('2013-10-18T08:53:14');
-
hidden_4003 about 10 yearsI'm using firebug >>> new Date('2013-10-18 08:53:14') Date {Invalid Date}
-
Sunil B N about 10 yearsnew Date() takes the former format too. // I'm using chrome
-
Sunil B N about 10 yearsThe ISO format is a simplification of the ISO 8601 extended format. The format is as follows:
YYYY-MM-DDTHH:mm:ss.sssZ
-
hidden_4003 about 10 yearsWell that simplification does not work in FF29 and IE11, so you should at least mention that.
-
Sunil B N about 10 yearsSorry about it.. I've added now.