In this MongoDB tutorial, we are going to learn “MongoDB date format” with examples. We will cover this by implementing different operations with examples. The following list topics we are going to cover in this tutorial:
- MongoDB date format query
- MongoDB date format dd/mm/yyyy
- MongoDB date format yyyy-mm-dd
- MongoDB date format default
- MongoDB ISO date format example
- MongoDB date format timezone
- MongoDB date format to string
- MongoDB UTC date format
- MongoDB convert string to date format
MongoDB date format query
The date query in MongoDB displays the current date as a string in the mongo shell. The current date will be returned as a date object by MongoDB, and the mongo shell will bind the date object to MongoDB’s isolate helper.
We can specify a specific date by passing it as an ISO-8601 date string, which is then passed to MongoDB’s new date() method with a range of 0 to 9999.
Various date format in MongoDB
- new date (<milliseconds>)
- new Date (<YYYY-mm-dd THH:MM:ss>)
- new Date (<YYYY-mm-dd>)
- new Date(“<YYYY-mm-ddTHH:MM:ssZ>”)
1. new date (<milliseconds>):
- A millisecond is an integer and It defines the number of milliseconds.
- A millisecond will convert the date into a standard date format.
Example:
In the following example, we will convert the milliseconds into the date format.
new Date(789456123)
Here, we have used 789456123 milliseconds and the output of these milliseconds is 1970-01-10.

2. new Date (<YYYY-mm-dd THH:MM:ss>):
In this format, we define the year, month, and date in full format and it will also display the hour, minute, and second in full format.
Example:
In the following example, we have used the date as (“2021-11-20T12:05:45”).
new Date("2021-11-20T12:05:45")
This code will display output as:

3. new Date (<YYYY-mm-dd>):
We will define a year, month, and date in full format.
Example:
In the following example, we have used the date as (“2021-11-21”).
new Date("2021-11-21")
This code will display output as:

4. new Date(“<YYYY-mm-ddTHH:MM:ssZ>”):
In this format, we define the year, month, and date in full format and it will also display the hour, minute, and second in full format.
Example:
In the following example, we have used the date as (“2021-10-13T12:05:45Z”).
new Date("2021-10-13T12:05:45Z")
This code will display output as:

Also, check: MongoDB order by date
MongoDB date format dd/mm/yyyy
In this topic, we will use the $dateToString aggregation function to convert a date object to a string according to a user-specified format. Here, we will covert the date into dd/mm/yyyy format and understand this with the help of an example.
The $dateToString expression has the following syntax:
{ $dateToString: {
date: <dateExpression>,
format: <formatString>,
timezone: <tzExpression>,
onNull: <expression>
} }
Field | Description |
---|---|
date | The date field is used to convert a date into a string. |
format | Optional, the format field is used for date format specification. |
timezone | Optional, the timezone field is used for the timezone of the operation result. |
onNull | Optional, the onNull field is used to return if the date is null or missing. |
The subsequent format specifiers are available for use in the <formatString>:
Specifiers | Description | Possible Values |
---|---|---|
%d | Day of the Month ( 2 digits, zero padded) | 01-31 |
%m | Month (2 digits, zero padded) | 01-12 |
%Y | Year (4 digits, zero padded) | 0000-9999 |
%w | Day of week (1-Sunday, 7-Saturday) | 1-7 |
%G | Year in ISO 8601 format New in version 3.4 | 0000-9999 |
And some more format specifiers are also available as per your requirement.
Example:
The following documents were inserted in the ‘blogs’ collection.
db.blogs.find()
{ "_id" : ObjectId("611de5c86cc7e05e5e7fe745"), "timestamp" : ISODate("2020-04-01T00:00:00Z") }
{ "_id" : ObjectId("611de5e06cc7e05e5e7fe746"), "timestamp" : ISODate("2021-11-11T05:24:05.343Z") }
{ "_id" : ObjectId("611de6196cc7e05e5e7fe747"), "timestamp" : ISODate("2020-05-02T00:00:00Z") }
Now, we will apply the following block of code to the ‘blogs’ collection to convert the timestamp field into this date format dd/mm/yyyy.
db.blogs.aggregate(
[
{
$project: {
dayMonthYear: { $dateToString: { format: "%d/%m/%Y", date: "$timestamp" } }
}
}
]
)
Here, the aggregation uses the $dateToString to return the date as dd/mm/yyyy formatted string.
Output:

Read: MongoDB find string contains
MongoDB date format yyyy-mm-dd
In this topic, we will use the dateToString aggregation function to convert a date object to a string according to a user-specified format. Here, we will covert the date into the yyyy-mm-dd format and understand this with the help of an example.
Note, if you want to learn more about the $dateToString then you can check out the previous topic.
Example:
The following documents will insert into the country collection.
db.country.insertMany([
{
"_id" : 101,
"UserName" : "Larry",
"UserMessage" : "Hi",
"UserMessagePostDate" : new Date("2021-10-10"),
"Country" : "United States of America"
},
{
"_id" : 102,
"UserName" : "Chris",
"UserMessage" : "Hello",
"UserMessagePostDate" : new Date("2021-10-28"),
"Country" : "Canada"
},
{
"_id" : 103,
"UserName" : "Robert",
"UserMessage" : "Bye",
"UserMessagePostDate" : new Date("2021-10-31"),
"Country" : " New Zealand"
}
])
Let’s check the execution:

After that, we will apply the below query to the collection to convert the date field into this date format yyyy-mm-dd.
db.country.aggregate(
[
{
$project: {
yearMonthDate: { $dateToString: { format: "%Y-%m-%d", date: "$UserMessagePostDate" } }
}
}
]
)
Output:

Read: How to change collection name in MongoDB
MongoDB date format default
In this topic, you will learn MongoDB date format default. MongoDB stores date in UTC by default. As you are in the Middle European Timezone this means there is either a one or two-hour difference depending on whether the date falls in or outside the Daylight Saving Time (DST) period. In 2021 DST started on the 14th of March and ended on the 7th of November.
Here, we will insert some documents into the collection with the default date field.
Example:
The following documents will insert into the collection.
db.detail.insertMany([
{
"_id" : 1,
"Name" : "Larry",
"Age" : 23,
"Date" : new Date(),
"Country" : "United States of America"
},
{
"_id" : 2,
"Name" : "Chris",
"Age" : 26,
"Date" : new Date(),
"Country" : "Canada"
},
{
"_id" : 3,
"Name" : "Robert",
"Age" : 28,
"Date" : new Date(),
"Country" : "Australia"
}
])
Here, we have defined the Date field in the documents new Date(). And, this will insert the default date (current date). We will use the find() to return the documents.

We have successfully inserted the documents into the collection and In this, you can check out the default date field as well.
Read: MongoDB find last inserted document
MongoDB ISO date format example
In this topic, we will understand the example of the ISO date format. The new Date() returns the current date as a Date object. The mongosh binds the Date object with the ISODate.
We can define a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date() constructor or the ISODate() function.
Example:
In this example, we will use the new Date(“<YYYY-mm-dd>”) and this will return the ISODate with the specified date.
The following documents will insert into the sales collection.
db.sales.insert(
{
"_id" : 1,
"item" : "Notebook",
"price" : 100,
"quantity" : 2,
"address" : "12, Kingston, New York 12401 United States",
"date" : new Date("2021-10-06")
}
)
Here, the date field new Date(“2021-10-06”) will return the ISODate with the specified date.
Output:

The find() method returns the documents of the collection and the pretty() method is used to display the documents into easy-to-read form.
Read: MongoDB compare two fields
MongoDB date format timezone
In this topic, you will learn about the MongoDB date format timezone. As we know that the MongoDB stores date in UTC by default.
If you are in the Middle European Timezone then this indicates that there is either a one or two-hour difference depending on whether the date falls in or outside the Daylight Saving Time (DST) period. In 2021 DST started on the 14th of March and ended on the 7th of November.
MongoDB usually stores date in UTC and then display them in the local format we will use the helper function the $dateToString or another aggregation function $dateToParts to deal with this depending on what you want to do.
Now, we will understand the MongoDB date format timezone with the help of an example.
Example 1:
The following documents were inserted into the timezone collection.
db.timezone.find()
{"_id": 1, "name": "Asya", "tz": "America/New_York" }
{"_id": 2, "name": "Stennie", "tz": "Australia/Sydney" }
{"_id": 3, "name": "Alex", "tz": "Australia/Sydney" }
{"_id": 4, "name": "Joe", "tz": "Europe/Dublin" }
Now, we will the below query to add a new date field in the documents as per their timezone.
db.timezone.aggregate(
{
$addFields : {
now : {
$dateToString : { date : new Date(), timezone : "$tz" }
}
}
}
)
Here, we have used the aggregate function where $addFields is used to add a new field “now” in the documents and the $dateToString is used to take the timezone as per the “tz” field.
Output:

Example 2:
The timezone can be defined using either the Olson timezone identifier (e.g. "Europe/London"
, "GMT"
) or the UTC offset (e.g. "+02:30"
, "-1030"
).
The following document was inserted into the pets collection.
db.pets.find()
{
"_id" : 786,
"name" : "Fetch",
"born" : ISODate("2021-10-31T23:30:15.123Z")
}
Olson Timezone Identifier
We will run the below query where date string in three different timezones, each using the Olson timezone IDs:
db.pets.aggregate(
[
{
$project: {
_id: 0,
UTC: { $dateToString: { format: "%Y-%m-%dT%H:%M", date: "$born", timezone: "UTC" } },
Honolulu: { $dateToString: { format: "%Y-%m-%dT%H:%M", date: "$born", timezone: "Pacific/Honolulu" } },
Auckland: { $dateToString: { format: "%Y-%m-%dT%H:%M", date: "$born", timezone: "Pacific/Auckland" } }
}
}
]
)
Output:

Read: MongoDB remove an element from the array
MongoDB date format to string
In this topic, you will learn to convert the MongoDB date format to string. We will use the $dateToString aggregation pipeline operator to convert a given date object to a string. Let’s more understand this with the help of an example.
Example:
The following document was inserted into the address collection.
db.address.find().pretty()
{
"_id" : ObjectId("61b06e976732b89acb7fc12f"),
"Name" : "Larry",
"Age" : 23,
"Date" : ISODate("2021-12-08T08:36:39.744Z"),
"Country" : "United States of America"
}
Now, we will apply the following code to return a date string from the Date field in that document.
db.address.aggregate(
[
{
$project: {
_id: 0,
dateString: { $dateToString: { format: "%Y-%m-%dT%H:%M:%S.%LZ", date: "$Date" } }
}
}
]
)
Here, we have used the aggregate function where we store string date into a new field “datestring” and the $dateToString function is used to return the date string as per defined format “%Y-%m-%dT%H:%M:%S.%LZ” from the Date field in that document.
Output:

Read: MongoDB find by ID
MongoDB UTC date format
In this topic, you will learn to convert the date into MongoDB UTC date format. We will use the $dateToString aggregate function to convert the date format into UTC.
Example:
The following document was inserted into the address collection. This is the same collection that we have used in the previous topic.
db.address.find().pretty()
{
"_id" : ObjectId("61b06e976732b89acb7fc12f"),
"Name" : "Larry",
"Age" : 23,
"Date" : ISODate("2021-12-08T08:36:39.744Z"),
"Country" : "United States of America"
}
Now, we will apply the following code to return a UTC date from the Date field in that document.
db.address.aggregate(
[
{
$project: {
_id: 0,
UTC: { $dateToString: { format: "%Y-%m-%dT%H:%M", date: "$Date", timezone: "UTC" } }
}
}
]
)
Here, we have used the aggregate function where we store string date into a new field “UTC”. After that, the $dateToString function is used to return the date string as per defined format “%Y-%m-%dT%H:%M “ and timezone UTC from the Date field in that document.
Output:

Read: Distinct query in MongoDB
MongoDB convert string to date format
In this topic, you will learn to convert string to date format. If you have dates saved as strings in a MongoDB collection, you can convert them to the Date BSON type if necessary.
There are basically 3 ways to convert a string to a date in MongoDB.
- The $dateFromString operator
- The $toDate operator
- The $convert operator
Example:
The following document was inserted into the dogs’ collection:
db.dogs.find()
{ "_id" : 1, "name" : "Rio", "born" : "2021-12-06T23:30:15.123" }
1. The $dateFromString operator:
The $dateFromString aggregation pipeline operator is used for converting a date to a string.
The following block of code is used to convert the string.
db.dogs.aggregate([
{
$project: {
born: {
$dateFromString: {
dateString: '$born'
}
}
}
}
])
Here, in the datestring argument, you have to define the string which you want to be converted to a date.
Output:

You can see that the date is now converted in the ISODate helper that means it is a Date object.
2. The $toDate operator:
The $toDate aggregation pipeline operator is used to convert a value to a date. Here, the value can be any type that can be converted to a date, which is numbers, strings, and objectIds.
The following block of code is used to convert strings to dates:
db.dogs.aggregate([
{
$project: {
"born": {
$toDate: "$born"
}
}
}
])
Output:

3. The $convert operator:
The $convert operator is individually designed for converting between one type and another. The field you want to convert is specified by the input parameter, and the type you want to convert it to is specified by the “to” parameter.
db.dogs.aggregate(
[
{
$project:
{
result:
{
$convert: {
input: "$born",
to: "date",
onError: "An error occurred",
onNull: "Input was null or empty"
}
}
}
}
]
)
Output:

Note:
The $convert operator holds the onError and onNull parameters that give a message to use in the event of an error or a null value. This block of aggregation operation provides for a user-friendly error message within the output document from stopping.
You may also like to read the following tutorials on MongoDB.
- Display MongoDB data in HTML
- MongoDB nested query
- MongoDB count with examples
- MongoDB aggregate $count
- MongoDB return only matching array elements
So, in this tutorial, we have learned about the MongoDB date format. And we have illustrated this topic using different examples. Here is the list of topics that we have discussed in this tutorial:
- MongoDB date format query
- MongoDB date format dd/mm/yyyy
- MongoDB date format yyyy-mm-dd
- MongoDB date format default
- MongoDB ISO date format example
- MongoDB date format timezone
- MongoDB date format to string
- MongoDB UTC date format
- MongoDB convert string to date format
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.