db.command.aggregate.dateFromParts
1. Operator Description
Function: Given date-related information, constructs and returns a date object.
Declaration: db.command.aggregate.dateFromParts(object)
2. Operator Parameters
Field | Type | Required | Description |
---|---|---|---|
year | number | No | Year |
month | number | No | Month |
day | number | No | Day |
hour | number | No | Hour |
minute | number | No | Minute |
second | number | No | Second |
millisecond | number | No | Millisecond |
timezone | string | No | Timezone |
isoWeekYear | number | No | isoWeekYear |
isoWeek | number | No | isoWeek |
isoDayOfWeek | number | No | isoDayOfWeek |
The syntax is as follows:
db.command.aggregate.dateFromParts({
year: <year>,
month: <month>,
day: <day>,
hour: <hour>,
minute: <minute>,
second: <second>,
millisecond: <ms>,
timezone: <tzExpression>
})
You can also use the ISO 8601 standard:
db.command.aggregate.dateFromParts({
isoWeekYear: <year>,
isoWeek: <week>,
isoDayOfWeek: <day>,
hour: <hour>,
minute: <minute>,
second: <second>,
millisecond: <ms>,
timezone: <tzExpression>
})
3. Sample Code
// Sample code in the Cloud Function environment
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)
}
The output is as follows:
{
"date": ISODate("2017-02-08T17:00:00.000Z")
}