Reputation: 4618
I have a timestamp of:
1399043514913
Which in reality is Fri, 02 May 2014 15:11:54 GMT
but I'm getting Sat, 05 Dec 46303 06:35:13 GMT
How can i correctly convert the timestamp to the Fri, 02 May
date?
I'm trying:
var dateTime = new Date(milliseconds * 1000);
var UTCString = new Date(dateTime).toUTCString();
What i really want to end up with is a Date object that i can use getDate()
, getMonth()
, etc on so i can put the date in any format i like.
Alternatively I'd like a way of converting 1399043514913
to 02/05/2014 15:11:54
Update:
Problem solved thanks to @JensB,
Here's my millseconds to date format converter as a result:
function formatTimeStamp(milliseconds) {
if (typeof milliseconds === "string")
milliseconds = parseInt(milliseconds.match(/\d+/g)[0]);
var dateTime = new Date(milliseconds);
var dateVar = new Date(dateTime);
var ISOString = new Date(dateVar).toISOString();
var UTCString = new Date(dateVar).toUTCString();
function pad(s) { return (s < 10) ? ("0" + s) : s.toString(); }
var dateTimeParts = {
dd: pad(dateVar.getDate()),
MM: pad(dateVar.getMonth() + 1),
yyyy: dateVar.getFullYear().toString(),
yy: dateVar.getFullYear().toString().substring(2, 4),
HH: pad(dateVar.getHours()),
mm: pad(dateVar.getMinutes()),
ss: pad(dateVar.getSeconds())
};
var execute = function(string) {
for (var key in dateTimeParts)
string = string.replace(key, dateTimeParts[key]);
return string;
};
return {
ISOString: ISOString,
UTCString: UTCString,
date: execute("dd/MM/yyyy"),
time: execute("HH:mm"),
dateTime: execute("dd/MM/yy HH:mm:ss"),
dateTimeParts: dateTimeParts
};
}
Upvotes: 1
Views: 379
Reputation: 6850
just running this code (same as in your question)
var dateTime = new Date(1399043514913);
var UTCString = new Date(dateTime).toUTCString();
alert(UTCString);
Gives me
Fri, 02 May 2014 15:11:54 GMT
Edit as per comment, this work?
$( document ).ready(function() {
var dateTime = new Date(1399043514913);
var dateVar = new Date(dateTime);
var UTCString = dateVar.toUTCString();
$("#dt").text(UTCString);
$("#dt2").text("Month: " + dateVar.getMonth());
$("#dt3").text("GetDate: " + dateVar.getDate());
});
Fiddler: http://jsfiddle.net/DqSGJ/3/
Upvotes: 3