将UNIX时间戳转换为日期时间(javascript) [英] Convert UNIX timestamp to date time (javascript)
本文介绍了将UNIX时间戳转换为日期时间(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
时间戳:
1395660658
代码:
//timestamp conversion
exports.getCurrentTimeFromStamp = function(timestamp) {
var d = new Date(timestamp);
timeStampCon = d.getDate() + '/' + (d.getMonth()) + '/' + d.getFullYear() + " " + d.getHours() + ':' + d.getMinutes();
return timeStampCon;
};
这会根据时间格式正确转换时间戳,但日期始终为:
This converts the time stamp properly in terms of time format, but the date is always:
17/0/1970
为什么 - 欢呼?
推荐答案
你需要乘以1000,因为自从纪元以来,以千分之一的JavaScript计数(这是(01/01/1970),而不是秒:
You have to multiply by 1000 as JavaScript counts in milliseconds since epoch (which is 01/01/1970), not seconds :
var d = new Date(timestamp*1000);
这篇关于将UNIX时间戳转换为日期时间(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文