Definition
$dateToString
Converts a date object to a string according to a user-specified format.
The
$dateToString
expression has the following operator expression syntax:
You can use $dateToString
for deployments hosted in the following environments:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
- MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
- MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
The $dateToString
expression has the following operator expression syntax:
{ $dateToString: {
date: <dateExpression>,
format: <formatString>,
timezone: <tzExpression>,
onNull: <expression>
} }
Field | Description | ||||||
---|---|---|---|---|---|---|---|
| The date to convert to string. | ||||||
| Optional. The date format specification. If unspecified and the If unspecified and the | ||||||
| Optional. The timezone of the operation result.
| ||||||
| Optional. The value to return if the If unspecified, |
Format Specifiers
The following format specifiers are available for use in the <formatString>
:
Specifiers | Description | Possible Values |
---|---|---|
| Abbreviated month name (3 letters) New in version 7.0. |
|
| Full month name New in version 7.0. |
|
| Day of month (2 digits, zero padded) |
|
| Year in ISO 8601 format |
|
| Hour (2 digits, zero padded, 24-hour clock) |
|
| Day of year (3 digits, zero padded) |
|
| Millisecond (3 digits, zero padded) |
|
| Month (2 digits, zero padded) |
|
| Minute (2 digits, zero padded) |
|
| Second (2 digits, zero padded) |
|
| Day of week number in ISO 8601 format (1-Monday, 7-Sunday) |
|
| Week of year (2 digits, zero padded) |
|
| Week of Year in ISO 8601 format |
|
| Day of week (1-Sunday, 7-Saturday) |
|
| Year (4 digits, zero padded) |
|
| The timezone offset from UTC. |
|
| The minutes offset from UTC as a number. For example, if the timezone offset ( |
|
| Percent Character as a Literal |
|
Example
Consider a sales
collection with the following document:
db.sales.insertOne(
{
"_id" : 1,
"item" : "abc",
"price" : 10,
"quantity" : 2,
"date" : ISODate("2014-01-01T08:15:39.736Z")
}
)
The following aggregation uses $dateToString
to return the date
field as formatted strings:
db.sales.aggregate(
[
{
$project: {
yearMonthDayUTC: { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
timewithOffsetNY: { $dateToString: { format: "%H:%M:%S:%L%z", date: "$date", timezone: "America/New_York"} },
timewithOffset430: { $dateToString: { format: "%H:%M:%S:%L%z", date: "$date", timezone: "+04:30" } },
minutesOffsetNY: { $dateToString: { format: "%Z", date: "$date", timezone: "America/New_York" } },
minutesOffset430: { $dateToString: { format: "%Z", date: "$date", timezone: "+04:30" } },
abbreviated_month: { $dateToString: {format: "%b", date: "$date", timezone: "+04:30" } },
full_month: { $dateToString: { format: "%B", date: "$date", timezone: "+04:30" } }
}
}
]
)
The operation returns the following result:
{
"_id" : 1,
"yearMonthDayUTC" : "2014-01-01",
"timewithOffsetNY" : "03:15:39:736-0500",
"timewithOffset430" : "12:45:39:736+0430",
"minutesOffsetNY" : "-300",
"minutesOffset430" : "270",
"abbreviated_month": "Jan",
"full_month": "January"
}