$dateToString (aggregation)

Definition

  • $dateToString

New in version 3.0.

Converts a date object to a string according to a user-specifiedformat.

The $dateToString expression has the followingoperator expression syntax:

  1. { $dateToString: {
  2. date: <dateExpression>,
  3. format: <formatString>,
  4. timezone: <tzExpression>,
  5. onNull: <expression>
  6. } }

The $dateToString takes a document with the following fields:

FieldDescriptiondate

Changed in version 3.6.

The date to convert to string. <dateExpression> must be avalid expression thatresolves to a Date, aTimestamp, or anObjectID.formatOptional. The date format specification. <formatString> can be anystring literal, containing 0 or more format specifiers. For alist of specifiers available, see Format Specifiers.

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

Changed in version 4.0: The format field is optional iffeatureCompatibilityVersion (fCV) is set to "4.0" orgreater. For more information on fCV, seesetFeatureCompatibilityVersion.

timezoneOptional. The timezone of the operation result.<tzExpression> must be a valid expression that resolves to a string formatted as eitheran Olson Timezone Identifier or aUTC Offset.If no timezone is provided, the result is displayed in UTC.

FormatExamplesOlson Timezone Identifier

  1. "America/New_York"
  2. "Europe/London"
  3. "GMT"

UTC Offset

  1. +/-[hh]:[mm], e.g. "+04:45"
  2. +/-[hh][mm], e.g. "-0530"
  3. +/-[hh], e.g. "+03"

New in version 3.6.

onNullOptional. The value to return if the date is null or missing.The arguments can be any valid expression.

If unspecified, $dateToString returns null if thedate is null or missing.

New in version 4.0: Requires featureCompatibilityVersion (fCV) set to"4.0" or greater. For more information on fCV, seesetFeatureCompatibilityVersion.

See also

$toString and $convert

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 formatNew in version 3.4.0000-9999
%HHour (2 digits, zero padded, 24-hour clock)00-23
%jDay of year (3 digits, zero padded)001-366
%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
%wDay of week (1-Sunday, 7-Saturday)1-7
%uDay of week number in ISO 8601 format (1-Monday, 7-Sunday)New in version 3.4.1-7
%UWeek of year (2 digits, zero padded)00-53
%VWeek of Year in ISO 8601 formatNew in version 3.4.01-53
%YYear (4 digits, zero padded)0000-9999
%zThe timezone offset from UTC.New in version 3.6.+/-[hh][mm]
%ZThe minutes offset from UTC as a number. For example, if thetimezone offset (+/-[hhmm]) was +0445, the minutesoffset is +285.New in version 3.6.+/-mmm
%%Percent Character as a Literal%

Example

Consider a sales collection with the following document:

  1. {
  2. "_id" : 1,
  3. "item" : "abc",
  4. "price" : 10,
  5. "quantity" : 2,
  6. "date" : ISODate("2014-01-01T08:15:39.736Z")
  7. }

The following aggregation uses $dateToString toreturn the date field as formatted strings:

  1. db.sales.aggregate(
  2. [
  3. {
  4. $project: {
  5. yearMonthDayUTC: { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
  6. timewithOffsetNY: { $dateToString: { format: "%H:%M:%S:%L%z", date: "$date", timezone: "America/New_York"} },
  7. timewithOffset430: { $dateToString: { format: "%H:%M:%S:%L%z", date: "$date", timezone: "+04:30" } },
  8. minutesOffsetNY: { $dateToString: { format: "%Z", date: "$date", timezone: "America/New_York" } },
  9. minutesOffset430: { $dateToString: { format: "%Z", date: "$date", timezone: "+04:30" } }
  10. }
  11. }
  12. ]
  13. )

The operation returns the following result:

  1. {
  2. "_id" : 1,
  3. "yearMonthDayUTC" : "2014-01-01",
  4. "timewithOffsetNY" : "03:15:39:736-0500",
  5. "timewithOffset430" : "12:45:39:736+0430",
  6. "minutesOffsetNY" : "-300",
  7. "minutesOffset430" : "270"
  8. }