跳到主要内容

db.command.aggregate.dateFromParts

1. 操作符描述#

功能:给定日期的相关信息,构建并返回一个日期对象。

声明:db.command.aggregate.dateFromParts(object)

2. 操作符参数#

字段类型必填说明
yearnumber
monthnumber
daynumber
hournumber小时
minutenumber分钟
secondnumber
millisecondnumber毫秒
timezonestring时区
isoWeekYearnumberisoWeekYear
isoWeeknumberisoWeek
isoDayOfWeeknumberisoDayOfWeek

语法如下:

db.command.aggregate.dateFromParts({    year: <year>,    month: <month>,    day: <day>,    hour: <hour>,    minute: <minute>,    second: <second>,    millisecond: <ms>,    timezone: <tzExpression>})

您也可以使用 ISO 8601 的标准:

db.command.aggregate.dateFromParts({    isoWeekYear: <year>,    isoWeek: <week>,    isoDayOfWeek: <day>,    hour: <hour>,    minute: <minute>,    second: <second>,    millisecond: <ms>,    timezone: <tzExpression>})

3. 示例代码#

// 云函数环境下示例代码const tcb = require('@cloudbase/node-sdk')const app = tcb.init({  env: 'xxx'})
const db = app.database()const $ = db.command.aggregate
exports.main = async (event, context) => {  const res = await db    .collection('dates')    .aggregate()    .project({      _id: 0,      date: $.dateFromParts({        year: 2017,        month: 2,        day: 8,        hour: 12,        timezone: 'America/New_York'      })    })    .end()  console.log(res.data)}

输出如下:

{  "date": ISODate("2017-02-08T17:00:00.000Z")}