有没有一种方法可以将unix格式的时间戳显示为ISODate? [英] Is there a way to display timestamp in unix format to ISODate?
问题描述
我们使用unix时间戳将日期存储在MongoDB中,执行查询时如何获取日期?有没有办法以ISODate格式显示时间戳?
We stored a date using unix timestamp in MongoDB, how do I get the date when I do the query? Is there a way to display timestamp in ISODate format?
推荐答案
背景
-
unixtime 值表示自时代以来的 seconds (1970年1月1日).
Background
A unixtime value represents seconds since the epoch (Jan 1, 1970).
A JavaScript Date()代表自纪元以来的毫秒.
A JavaScript Date() represents milliseconds since the epoch.
在MongoDB中,
ISODate()
是Date()
的便捷包装,它允许您从mongo
外壳程序中的ISO字符串创建日期.如果在外壳中使用new Date()
,它将返回ISODate()
.In MongoDB,
ISODate()
is a convenience wrapper forDate()
that allows you to create dates from ISO strings in themongo
shell. If you usenew Date()
in the shell, it will return anISODate()
.要在
unixtime
和ISODate()之间进行转换,可以将Unix时间戳乘以1000,然后将此值传递给new Date()
构造函数.To convert between a
unixtime
and an ISODate() you can multiply your unix timestamps by 1000 and pass this value to thenew Date()
constructor.> db.mydata.insert({ unixtime: 1362143511 }) > var doc = db.mydata.findOne(); // convert unixtime seconds to milliseconds and create JS date > var date = new Date(doc.unixtime * 1000); > date ISODate("2013-03-01T13:11:51Z")
这篇关于有没有一种方法可以将unix格式的时间戳显示为ISODate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!