$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
.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.
- an Olson Timezone Identifier , such as
If the dateStringExpression
or tzExpression
evaluate to null, missing or undefined, the result of the $dateFromString expression is null.
Example
Consider a collection logmessages
that contains the following documents:
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.