Javascript 人类可读格式的时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5416920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timestamp to human readable format
提问by georgevich
Well I have a strange problem while convert from unix timestamp to human representation using javascript
好吧,我在使用 javascript 从 unix 时间戳转换为人类表示时遇到了一个奇怪的问题
Here is timestamp
这是时间戳
1301090400
This is my javascript
这是我的javascript
var date = new Date(timestamp * 1000);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDay();
var hour = date.getHours();
var minute = date.getMinutes();
var seconds = date.getSeconds();
I expected results to be 2011 2, 25 22 00 00. But it is 2011, 2, 6, 0, 0, 0 What I miss ?
我预计结果是 2011 2, 25 22 00 00。但它是 2011, 2, 6, 0, 0, 0 我想念什么?
回答by KooiInc
getDay()
returns the day of the week. To get the date, use date.getDate()
. getMonth()
retrieves the month, but month is zero based, so using getMonth()+1
should give you the right month. Time value seems to be ok here, albeit the hour is 23 here (GMT+1). If you want universal values, add UTC
to the methods (e.g. date.getUTCFullYear()
, date.getUTCHours()
)
getDay()
返回星期几。要获取日期,请使用date.getDate()
. getMonth()
检索月份,但月份是从零开始的,所以使用getMonth()+1
应该给你正确的月份。时间值在这里似乎没问题,尽管这里的小时是 23 (GMT+1)。如果您想要通用值,请添加UTC
到方法中(例如date.getUTCFullYear()
, date.getUTCHours()
)
var timestamp = 1301090400,
date = new Date(timestamp * 1000),
datevalues = [
date.getFullYear(),
date.getMonth()+1,
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
];
alert(datevalues); //=> [2011, 3, 25, 23, 0, 0]
回答by Shouvik
var newDate = new Date();
newDate.setTime(unixtime*1000);
dateString = newDate.toUTCString();
Where unixtime
is the time returned by your sql db. Here is a fiddleif it helps.
unixtime
你的sql db返回的时间在哪里。如果有帮助,这里有一个小提琴。
For example, using it for the current time:
例如,在当前时间使用它:
document.write( new Date().toUTCString() );
回答by golakers
here is kooilnc's answer w/ padded 0's
这是 kooilnc 的答案,带有填充的 0
function getFormattedDate() {
var date = new Date();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
month = (month < 10 ? "0" : "") + month;
day = (day < 10 ? "0" : "") + day;
hour = (hour < 10 ? "0" : "") + hour;
min = (min < 10 ? "0" : "") + min;
sec = (sec < 10 ? "0" : "") + sec;
var str = date.getFullYear() + "-" + month + "-" + day + "_" + hour + ":" + min + ":" + sec;
/*alert(str);*/
return str;
}
回答by Hossein
Hours, minutes and seconds depend on the time zone of your operating system.In GMT (UST) it's 22:00:00 but in different timezones it can be anything. So add the timezone offset to the time to create the GMT date:
小时、分钟和秒取决于操作系统的时区。在 GMT (UST) 中,它是 22:00:00,但在不同的时区,它可以是任何东西。因此,将时区偏移量添加到时间以创建 GMT 日期:
var d = new Date();
date = new Date(timestamp*1000 + d.getTimezoneOffset() * 60000)