How to Format the Date in MongoDB

If you have documents that store dates as Date objects, but you want to return them in a different format, you can use the $dateToString aggregate pipeline operator.

For example, you might want a date to be returned in mm/dd/yyyy format instead of the long ISODate() format that includes minutes, seconds, milliseconds, etc

The $dateToString operator converts the Date object to a string, and optionally allows you to specify a format for the resulting output.

Example

Suppose we have a collection called cats with the following documents:

{ "_id" : 1, "name" : "Scratch", "born" : ISODate("2021-01-03T23:30:15.123Z") }
{ "_id" : 2, "name" : "Meow", "born" : ISODate("2019-12-08T04:00:20.112Z") }
{ "_id" : 3, "name" : "Fluffy", "born" : ISODate("2020-09-24T10:45:01.007Z") }

We can use $dateToString to return that document with the dates in a different format.

For example, let’s remove the seconds and milliseconds from the date:

db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)

Result:

{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
{ "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
{ "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }

We were able to format the date by using the format parameter. This is an optional parameter that allows you to use zero or more format specifiers to indicate how the date should be formatted.

See MongoDB $dateToString Format Specifiers for a full list of format specifiers that can be used with the $dateToString operator.

Date in dd/mm/yyyy Format

Here’s another example that converts the dates to dd/mm/yyyy format:

db.cats.aggregate(
   [
     {
       $project: {
          _id: 0,
          formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
       }
     }
   ]
)

Result:

{ "formattedDate" : "03/01/2021" }
{ "formattedDate" : "08/12/2019" }
{ "formattedDate" : "24/09/2020" }

Date in mm/dd/yyyy Format

Or to put it in mm/dd/yyyy format, we can simply switch the first two format specifiers around:

db.cats.aggregate(
   [
     {
       $project: {
          _id: 0,
          formattedDate: { $dateToString: { format: "%m/%d/%Y", date: "$born" } }
       }
     }
   ]
)

Result:

{ "formattedDate" : "01/03/2021" }
{ "formattedDate" : "12/08/2019" }
{ "formattedDate" : "09/24/2020" }

Return a Single Date Part

You can include as many or as few format specifiers as you wish. For example, you could use just one format specifier to output just the year part of the date.

Example:

db.cats.aggregate(
   [
     {
       $project: {
          _id: 0,
          formattedDate: { $dateToString: { format: "%Y", date: "$born" } }
       }
     }
   ]
)

Result:

{ "formattedDate" : "2021" }
{ "formattedDate" : "2019" }
{ "formattedDate" : "2020" }

Although, bear in mind that there are other ways to extract just a single date part from a Date object. For example, you can use the $year operator to extract the year.

Here are the various operators for extracting each specific date part:

You can also use the $dateToParts operator to return a document that contains all the various date parts separated into their own field.