将毫秒转换为日期 (jQuery/JavaScript) [英] Converting milliseconds to a date (jQuery/JavaScript)
问题描述
我有点游手好闲,但我会尽量保持清楚-
I'm a bit of a rambler, but I'll try to keep this clear -
我很无聊,所以我正在开发一个留言箱",但我对一件事有点困惑.我想获取输入消息的时间,并且我想确保我正在获取服务器时间,或者至少确保我没有获取用户的本地时间.我知道没关系,因为这东西除了我以外不会被任何人使用,但我想彻底.我环顾四周并测试了一些东西,我认为唯一的方法是获取自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数,因为那将是每个人都一样.
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.
我就是这样做的:
var time = new Date();
var time = time.getTime();
返回一个类似 1294862756114
的数字.
That returns a number like 1294862756114
.
有没有办法将 1294862756114
转换为更具可读性的日期,例如 DD/MM/YYYY HH:MM:SS
?
Is there a way to convert 1294862756114
to a more readable date, like DD/MM/YYYY HH:MM:SS
?
所以,基本上,我正在寻找与 PHP 的 date();
函数等效的 JavaScript.
So, basically, I'm looking for JavaScript's equivalent of PHP's date();
function.
推荐答案
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)
这篇关于将毫秒转换为日期 (jQuery/JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!