这就是我在Ruby中所做的。
time = Time.now
=> 2013-10-08 12:32:50 +0530
time.to_i //converts time to integer
=> 1381215770
Time.at(time.to_i) //converts integer to time
=> 2013-10-08 12:32:50 +0530
我正在尝试使用Node.js实现相同的功能,但不知道该怎么做。请帮我找到一个用Node.js,Javascript实现相同的模块。谢谢!
答案 0 :(得分:10)
在javascript世界中。
Date.now()
and
new Date(1381216317325);
答案 1 :(得分:4)
答案 2 :(得分:-1)
new Date().getTime();
将返回一个整数,表示自UTC时间1970年1月1日午夜以来所花费的时间(以毫秒为单位)。这需要以某种方式解析为更具人性化。
在Javascript中没有实现将此数字转换为人类可解释日期的默认方法,因此您必须自己编写。
一个简单的方法是:
function getTime() {
var now = new Date();
return ((now.getMonth() + 1) + '-' +
(now.getDate()) + '-' +
now.getFullYear() + " " +
now.getHours() + '-' +
((now.getMinutes() < 10)
? ("0" + now.getMinutes())
: (now.getMinutes())) + ':' +
((now.getSeconds() < 10)
? ("0" + now.getSeconds())
: (now.getSeconds())));
}
console.log(getTime());
您可以自己调整外观顺序。