$dateFromParts
New from version 5.0.1 and 8.0
The $dateFromParts aggregation operator in Amazon DocumentDB constructs and
returns a date and time value from its constituent parts, such as the year, month,
day, hour, minute, second, and millisecond.
Parameters
-
year: The calendar year. Required when not using ISO 8601 parts. -
month,day,hour,minute,second,millisecond: (optional) The remaining date and time components. Omitted components default to their lowest valid value. -
isoWeekYear,isoWeek,isoDayOfWeek: (optional) ISO 8601 date components. Use these instead ofyear,month, anddayto construct a date from an ISO 8601 week date. -
timezone: (optional) The time zone to use when constructing the date. If not specified, UTC is used.
Example (MongoDB Shell)
The following example demonstrates how to use the $dateFromParts
operator to construct a date from individual components.
Create sample documents
db.parts.insertMany([ { _id: 1, y: 2023, m: 4, d: 1 }, { _id: 2, y: 2023, m: 12, d: 25 } ]);
Query example
db.parts.aggregate([ { $project: { _id: 1, constructedDate: { $dateFromParts: { year: "$y", month: "$m", day: "$d" } } } } ])
Output
[
{ "_id": 1, "constructedDate": ISODate("2023-04-01T00:00:00Z") },
{ "_id": 2, "constructedDate": ISODate("2023-12-25T00:00:00Z") }
]
Code examples
To view a code example for using the $dateFromParts command, choose the tab for the language that you want to use: