将毫秒转换为日期(jQuery / JavaScript) [英] Converting milliseconds to a date (jQuery/JavaScript)
问题描述
我有点像漫步者,但我会尽力保持这一点 -
I'm a bit of a rambler, but I'll try to keep this clear -
我很无聊,所以我正在努力shoutbox,我有点困惑一件事。我希望得到一条消息输入的时间,我想确保我得到服务器时间,或者至少确保我没有得到用户的本地时间。我知道这没关系,因为除了我之外,任何人都不会使用这个东西,但我想要彻底。我环顾四周并测试了一些东西,我认为这样做的唯一方法就是从?/?/ 1970或者其他任何东西获得毫秒数,因为对每个人来说都是一样的。
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 ?/?/1970 or whatever it is, since that'd be the same for everyone.
我这样做:
var time = new Date();
var time = time.getTime();
返回类似 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
?
所以,基本上,我正在寻找JavaScript相当于PHP的日期();
function。
So, basically, I'm looking for JavaScript's equivalent of PHP's date();
function.
推荐答案
var time = new Date().getTime();
var date = new Date(time);
alert(date.toString()); // Wed Jan 12 2011 12:42:46 GMT-0800 (PST)
这篇关于将毫秒转换为日期(jQuery / JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!