将毫秒转换为日期(jQuery / JS) [英] Converting milliseconds to a date (jQuery/JS)

查看:134
本文介绍了将毫秒转换为日期(jQuery / JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个漫步者,但我会尽量保持清楚 -



我很无聊,所以我正在做一个 shoutbox,而且我有一点点困惑。我想得到消息输入的时间,我想确保我得到服务器的时间,或者至少确保我没有得到用户的本地时间。我知道这没关系,因为除了我以外,这个东西不会被任何人使用,但是我想要彻底。我已经环顾四周,测试了一些事情,我认为唯一的方法是从?/?/ 1970或者任何它所获得的毫秒数,因为每个人都是一样的。



我这样做是这样的:

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

返回一个数字,如 1294862756114 。 / p>

有没有办法将 1294862756114 转换为更可读的日期,如 DD / MM / YYYY HH:MM:SS



所以,基本上,我正在寻找PHP的 date(); 函数的JavaScripts。 >

解决方案

  var time = new Date()。getTime(); 
var date = new Date(time);
alert(date.toString()); // Wed Jan 12 2011 12:42:46 GMT-0800(PST)


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 ?/?/1970 or whatever it is, 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 JavaScripts equivelant 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 / JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆