Skip to main content

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

FieldTypeRequiredDescription
yearnumberNoYear
monthnumberNoMonth
daynumberNoDay
hournumberNoHour
minutenumberNoMinute
secondnumberNoSecond
millisecondnumberNoMillisecond
timezonestringNoTimezone
isoWeekYearnumberNoisoWeekYear
isoWeeknumberNoisoWeek
isoDayOfWeeknumberNoisoDayOfWeek

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")
}