$dateFromString (aggregation)

Definition

  • $dateFromString

New in version 3.6.

Converts a date/time string to a date object.

The $dateFromString expression has the following syntax:

  1. { $dateFromString: {
  2. dateString: <dateStringExpression>,
  3. format: <formatStringExpression>,
  4. timezone: <tzExpression>,
  5. onError: <onErrorExpression>,
  6. onNull: <onNullExpression>
  7. } }

The $dateFromString takes a document with the following fields:

FieldDescriptiondateStringThe date/time string to convert to a date object. SeeDate formore information on date/time formats.

Note

If specifying the timezone option to the operator, do not includetime zone information in the dateString.

formatOptional. The date format specification of thedateString. The format can be any expressionthat evaluates to a string literal,containing 0 or more format specifiers. For a list ofspecifiers available, seeFormat Specifiers.

If unspecified, $dateFromString uses"%Y-%m-%dT%H:%M:%S.%LZ" as the default format.

New in version 4.0.

timezoneOptional. 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 Zulutime (UTC time zone), you cannot specify the timezone argument.

<timezone> allows for the following options and expressionsthat 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, seeExpressions.onErrorOptional. If $dateFromString encounters an error whileparsing the given dateString, it outputs the result valueof the provided onError expression.This result value can be of any type.

If you do not specify onError, $dateFromStringthrows an error if it cannot parse dateString.onNullOptional. If the dateString provided to$dateFromString is null or missing, it outputsthe result value of the provided onNullexpression.This result value can be of any type.

If you do not specify onNull and dateString is nullor missing, then $dateFromString outputs null.

See also

$toDate and $convert

Behavior

ExampleResults
  1. { $dateFromString: { dateString: "2017-02-08T12:10:40.787"} }
ISODate("2017-02-08T12:10:40.787Z")
  1. { $dateFromString: { dateString: "2017-02-08T12:10:40.787", timezone: "America/New_York"} }
ISODate("2017-02-08T17:10:40.787Z")
  1. { $dateFromString: { dateString: "2017-02-08"} }
ISODate("2017-02-08T00:00:00Z")
  1. { $dateFromString: { dateString: "06-15-2018", format: "%m-%d-%Y"} }
ISODate("2018-06-15T00:00:00Z")
  1. { $dateFromString: { dateString: "15-06-2018", format: "%d-%m-%Y"} }
ISODate("2018-06-15T00:00:00Z")

Format Specifiers

The following format specifiers are available for use in the<formatString>:

SpecifiersDescriptionPossible Values
%dDay of Month (2 digits, zero padded)01-31
%GYear in ISO 8601 format0000-9999
%HHour (2 digits, zero padded, 24-hour clock)00-23
%LMillisecond (3 digits, zero padded)000-999
%mMonth (2 digits, zero padded)01-12
%MMinute (2 digits, zero padded)00-59
%SSecond (2 digits, zero padded)00-60
%uDay of week number in ISO 8601 format (1-Monday, 7-Sunday)1-7
%VWeek of Year in ISO 8601 format1-53
%YYear (4 digits, zero padded)0000-9999
%zThe timezone offset from UTC.+/-[hh][mm]
%ZThe minutes offset from UTC as a number. For example, if thetimezone offset (+/-[hhmm]) was +0445, the minutesoffset is +285.+/-mmm
%%Percent Character as a Literal%

Examples

Converting Dates

Consider a collection logmessages that contains the followingdocuments with dates.

  1. { _id: 1, date: "2017-02-08T12:10:40.787", timezone: "America/New_York", message: "Step 1: Started" },
  2. { _id: 2, date: "2017-02-08", timezone: "-05:00", message: "Step 1: Ended" },
  3. { _id: 3, message: " Step 1: Ended " },
  4. { _id: 4, date: "2017-02-09", timezone: "Europe/London", message: "Step 2: Started"}
  5. { _id: 5, date: "2017-02-09T03:35:02.055", timezone: "+0530", message: "Step 2: In Progress"}

The following aggregation uses $dateFromString to convert the date valueto a date object:

  1. db.logmessages.aggregate( [ {
  2. $project: {
  3. date: {
  4. $dateFromString: {
  5. dateString: '$date',
  6. timezone: 'America/New_York'
  7. }
  8. }
  9. }
  10. } ] )

The above aggregation returns the following documents and converts each date fieldto the Eastern Time Zone:

  1. { "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
  2. { "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
  3. { "_id" : 3, "date" : null }
  4. { "_id" : 4, "date" : ISODate("2017-02-09T05:00:00Z") }
  5. { "_id" : 5, "date" : ISODate("2017-02-09T08:35:02.055Z") }

The timezone argument can also be provided through a document field instead of ahard coded argument. For example:

  1. db.logmessages.aggregate( [ {
  2. $project: {
  3. date: {
  4. $dateFromString: {
  5. dateString: '$date',
  6. timezone: '$timezone'
  7. }
  8. }
  9. }
  10. } ] )

The above aggregation returns the following documents and converts each date fieldto their respective UTC representations.

  1. { "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
  2. { "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
  3. { "_id" : 3, "date" : null }
  4. { "_id" : 4, "date" : ISODate("2017-02-09T00:00:00Z") }
  5. { "_id" : 5, "date" : ISODate("2017-02-08T22:05:02.055Z") }

onError

If your collection contains documents with unparsable date strings,$dateFromString throws an error unless you provide anaggregation expression to the optionalonError parameter.

For example, given a collection dates with the followingdocuments:

  1. { "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
  2. { "_id" : 2, "date" : "20177-02-09T03:35:02.055", timezone: "America/New_York" }

You can use the onError parameter to return the invalid date inits original string form:

  1. db.dates.aggregate( [ {
  2. $project: {
  3. date: {
  4. $dateFromString: {
  5. dateString: '$date',
  6. timezone: '$timezone',
  7. onError: '$date'
  8. }
  9. }
  10. }
  11. } ] )

This returns the following documents:

  1. { "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
  2. { "_id" : 2, "date" : "20177-02-09T03:35:02.055" }

onNull

If your collection contains documents with null date strings,$dateFromString returns null unless you provide anaggregation expression to the optionalonNull parameter.

For example, given a collection dates with the followingdocuments:

  1. { "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
  2. { "_id" : 2, "date" : null, timezone: "America/New_York" }

You can use the onNull parameter to have $dateFromStringreturn a date representing the unix epoch instead of null:

  1. db.dates.aggregate( [ {
  2. $project: {
  3. date: {
  4. $dateFromString: {
  5. dateString: '$date',
  6. timezone: '$timezone',
  7. onNull: new Date(0)
  8. }
  9. }
  10. }
  11. } ] )

This returns the following documents:

  1. { "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
  2. { "_id" : 2, "date" : ISODate("1970-01-01T00:00:00Z") }