- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $dateFromString (aggregation)
$dateFromString (aggregation)¶
On this page
Definition¶
-
$dateFromString
¶ New in version 3.6.
Converts a date/time string to a date object.
The
$dateFromString
expression has the following syntax:The
$dateFromString
takes a document with the following fields:Field Description dateString
The date/time string to convert to a date object. See Date for more information on date/time formats.
Note
If specifying the
timezone
option to the operator, do not include time zone information in thedateString
.format
Optional. The date format specification of the
dateString
. Theformat
can be any expression that evaluates to a string literal, containing 0 or more format specifiers. For a list of specifiers available, see Format Specifiers.If unspecified,
$dateFromString
uses"%Y-%m-%dT%H:%M:%S.%LZ"
as the default format.New in version 4.0.
timezone
Optional. The time zone to use to format the date.
Note
If the
dateString
argument is formatted like ‘2017-02-08T12:10:40.787Z’, in which the ‘Z’ at the end indicates Zulu time (UTC time zone), you cannot specify thetimezone
argument.<timezone>
allows for the following options and expressions that evaluate to them:- 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"
, or
- The strings “Z”, “UTC”, or “GMT”
For more information on expressions, see Expressions.
onError
Optional. If
$dateFromString
encounters an error while parsing the givendateString
, it outputs the result value of the providedonError
expression. This result value can be of any type.If you do not specify
onError
,$dateFromString
throws an error if it cannot parsedateString
.onNull
Optional. If the
dateString
provided to$dateFromString
isnull
or missing, it outputs the result value of the providedonNull
expression. This result value can be of any type.If you do not specify
onNull
anddateString
isnull
or missing, then$dateFromString
outputsnull
.- an Olson Timezone Identifier,
such as
Behavior¶
Example | Results |
---|---|
ISODate("2017-02-08T12:10:40.787Z") |
|
ISODate("2017-02-08T17:10:40.787Z") |
|
ISODate("2017-02-08T00:00:00Z") |
|
ISODate("2018-06-15T00:00:00Z") |
|
ISODate("2018-06-15T00:00:00Z") |
Format Specifiers¶
The following format specifiers are available for use in the
<formatString>
:
Specifiers | Description | Possible Values |
---|---|---|
%d |
Day of Month (2 digits, zero padded) | 01 -31 |
%G |
Year in ISO 8601 format | 0000 -9999 |
%H |
Hour (2 digits, zero padded, 24-hour clock) | 00 -23 |
%L |
Millisecond (3 digits, zero padded) | 000 -999 |
%m |
Month (2 digits, zero padded) | 01 -12 |
%M |
Minute (2 digits, zero padded) | 00 -59 |
%S |
Second (2 digits, zero padded) | 00 -60 |
%u |
Day of week number in ISO 8601 format (1-Monday, 7-Sunday) | 1 -7 |
%V |
Week of Year in ISO 8601 format | 1 -53 |
%Y |
Year (4 digits, zero padded) | 0000 -9999 |
%z |
The timezone offset from UTC. | +/-[hh][mm] |
%Z |
The minutes offset from UTC as a number. For example, if the
timezone offset (+/-[hhmm] ) was +0445 , the minutes
offset is +285 . |
+/-mmm |
%% |
Percent Character as a Literal | % |
Examples¶
Converting Dates¶
Consider a collection logmessages
that contains the following
documents with dates.
The following aggregation uses $dateFromString to convert the date
value
to a date object:
The above aggregation returns the following documents and converts each date
field
to the Eastern Time Zone:
The timezone
argument can also be provided through a document field instead of a
hard coded argument. For example:
The above aggregation returns the following documents and converts each date
field
to their respective UTC representations.
onError
¶
If your collection contains documents with unparsable date strings,
$dateFromString
throws an error unless you provide an
aggregation expression to the optional
onError
parameter.
For example, given a collection dates
with the following
documents:
You can use the onError
parameter to return the invalid date in
its original string form:
This returns the following documents:
onNull
¶
If your collection contains documents with null
date strings,
$dateFromString
returns null
unless you provide an
aggregation expression to the optional
onNull
parameter.
For example, given a collection dates
with the following
documents:
You can use the onNull
parameter to have $dateFromString
return a date representing the unix epoch instead of null
:
This returns the following documents: