$dateFromParts (aggregation)
On this page
Definition
-
$dateFromParts
-
New in version 3.6.
Constructs and returns a Date object given the date’s constituent properties.
The
$dateFromParts
expression has the following syntax:You can also specify your constituent date fields in ISO week date format using the following syntax:
The
$dateFromParts
takes a document with the following fields:Field Required/Optional Description Possible Values year
Required if not using isoWeekYear
Calendar year. 0
-9999
isoWeekYear
Required if not using year
ISO Week Date Year. 0
-9999
month
Optional. Can only be used with year
.Month. Defaults to 1
.1
-12
isoWeek
Optional. Can only be used with isoWeekYear
.Week of year. Defaults to 1
.1
-53
day
Optional. Can only be used with year
.Day of month. Defaults to 1
.1
-31
isoDayOfWeek
Optional. Can only be used with isoWeekYear
.Day of week (Monday 1
- Sunday7
). Defaults to1
.1
-7
hour
Optional Hour. Defaults to 0
.0
-23
minute
Optional Minute. Defaults to 0
.0
-59
second
Optional Second. Defaults to 0
.0
-59
millisecond
Optional Millisecond. Defaults to 0
.0
-999
timezone
Optional <timezone>
can be any expression that evaluates to a string whose value is either:- an Olson Timezone Identifier , such as
"Europe/London"
or"America/New_York"
, or - a UTC offset in the form:
+/-[hh]:[mm]
, e.g."+04:45"
, or+/-[hh][mm]
, e.g."-0530"
, or+/-[hh]
, e.g."+03"
.
For more information on expressions, see Expressions.
Timezone expression Important
You cannot combine the use of calendar dates and ISO week date fields when constructing your
$dateFromParts
input document. - an Olson Timezone Identifier , such as
Behavior
When using an Olson Timezone Identifier in the <timezone>
field, MongoDB applies the DST offset if applicable for the specified timezone.
For example, consider a sales
collection with the following document:
The following aggregation illustrates how MongoDB handles the DST offset for the Olson Timezone Identifier. The example uses the $hour
and $minute
operators to return the corresponding portions of the date
field:
The operation returns the following result:
Example
The following aggregation uses $dateFromParts
to construct three date objects from the provided input fields:
The operation returns the following result: