Javascript – Converting milliseconds to a date (jQuery/JavaScript)

datetimejavascriptjquery

I'm a bit of a rambler, but I'll try to keep this clear –

I'm bored, so I'm working on a "shoutbox", and I'm a little confused over one thing. I want to get the time that a message is entered, and I want to make sure I'm getting the server time, or at least make sure I'm not getting the local time of the user. I know it doesn't matter, since this thing won't be used by anyone besides me, but I want to be thorough. I've looked around and tested a few things, and I think the only way to do this is to get the milliseconds since January 1, 1970 00:00:00 UTC, since that'd be the same for everyone.

I'm doing that like so:

var time = new Date();
var time = time.getTime();

That returns a number like 1294862756114.

Is there a way to convert 1294862756114 to a more readable date, like DD/MM/YYYY HH:MM:SS?

So, basically, I'm looking for JavaScript's equivalent of PHP's date(); function.

Best Answer

var time = new Date().getTime(); // get your number
var date = new Date(time); // create Date object

console.log(date.toString()); // result: Wed Jan 12 2011 12:42:46 GMT-0800 (PST)