On this page本页内容
Date()
Returns a date either as a string or as a Date object.以字符串或Date对象的形式返回日期。
Date()
mongosh
.mongosh
字符串形式返回当前日期。new Date()
mongosh
ISODate
helper. ISODate
辅助对象包装Date对象。ISODate
is in UTC.ISODate
以UTC为标准。You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 您可以通过向新的0
through 9999
to the new Date()
constructor or the ISODate()
function. date()
构造函数或ISODate()
函数传递一个ISO-8601日期字符串来指定特定日期,该字符串的年份在0
到9999
之间。These functions accept the following formats:这些函数接受以下格式:
new Date("<YYYY-mm-dd>")
ISODate
with the specified date.ISODate
。new Date("<YYYY-mm-ddTHH:MM:ss>")
ISODate
with the specified datetime in UTC.ISODate
。new Date("<YYYY-mm-ddTHH:MM:ssZ>")
ISODate
with the specified datetime in UTC.ISODate
。new Date(<integer>)
ISODate
instance.Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).在内部,Date对象存储为有符号的64位整数,表示自Unix纪元(1970年1月1日)以来的毫秒数。
Not all database operations and drivers support the full 64-bit range. 并非所有数据库操作和驱动程序都支持完整的64位范围。You may safely work with dates with years within the inclusive range 您可以安全地使用年数在0到9999范围内的日期。0
through 9999
.
If no document with 如果_id
equal to 1
exists in the products
collection, the following operation inserts a document with the field dateAdded
set to the current date:products
集合中不存在_id
等于1
的文档,则以下操作将插入一个字段dateAdded
设置为当前日期的文档:
db.products.updateOne( { _id: 1 }, { $set: { item: "apple" }, $setOnInsert: { dateAdded: new Date() } }, { upsert: true } )
$currentDate
To return the date as a string, use the 要以字符串形式返回日期,请使用Date()
method, as in the following example:Date()
方法,如下例所示:
var myDateString = Date();
Date
ObjectDate
对象mongosh
wraps objects of Date type with the 使用ISODate
helper; however, the objects remain of type Date.ISODate
助手包装Date类型的对象;但是,对象仍然是Date类型。
The following example uses 以下示例使用new Date()
to return Date object with the specified UTC datetime.new Date()
返回指定UTC日期时间的Date对象。
var myDate = new Date("2016-05-18T16:00:00Z");