NameDescriptionReturns the day of the year for a date as a number between 1 and 366 (leap year).Returns the day of the month for a date as a number between 1 and 31.Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).Returns the year for a date as a number (e.g. 2014).Returns the month for a date as a number between 1 (January) and 12 (December).Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year).Returns the hour for a date as a number between 0 and 23.Returns the minute for a date as a number between 0 and 59.Returns the seconds for a date as a number between 0 and 60 (leap seconds).Returns the milliseconds of a date as a number between 0 and 999.Returns the date as a formatted string.Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).Returns the week number in ISO 8601 format, ranging from 1 to 53. Week numbers start at 1 with the week (Monday through Sunday) that contains the year’s first Thursday.Returns the year number in ISO 8601 format. The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601).
#2021-02-25 and @2021-02-25 are not the same. First refers to a duration (during that day from midnight to midnight) while the second points to the beginning of that day (a specific moment).
Date and time data is commonly managed by database systems and is incredibly important, but can often be trickier to handle correctly than it initially appears. Databases must be able to store date and time data in clear, unambiguous formats, transform that data into user-friendly formats to interact with client applications, and perform time-based operations taking into account complexities like different timezones and changes in daylight savings time.
In this guide, we'll discuss some of the tools that MongoDB provides to work effectively with date and time data. We'll explore relevant data types, take a look at the operators and methods, and go over how to best use these tools to keep your date and time data in good order.
RELATED ON PRISMA.IO
If you are using MongoDB with Prisma, you can use the MongoDB connector to connect to and manage your database. Prisma's
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 type to MongoDB's
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type.
Prisma is an open-source database toolkit for Typescript and Node.js that aims to make app developers more productive and confident when working with databases.
The MongoDB use temp_db
db.dates.insertMany([ { name: "Created with `Date()`", date: Date(), }, { name: "Created with `new Date()`", date: new Date(), }, { name: "Created with `ISODate()`", date: ISODate(), },])8 and { "acknowledged" : true, "insertedIds" : [ ObjectId("62726af5a3dc7398b97e6e93"), ObjectId("62726af5a3dc7398b97e6e94"), ObjectId("62726af5a3dc7398b97e6e95") ]}0 types
The can store date and time values as a combined unit.
Here, the left column represents the BSON (binary JSON) name for the data type and the second column represents the ID number associated with that type. The final "Alias" column represents the string that MongoDB uses to represent the type:
Type | Number | Alias |
------------------ | ------ | ------------ |
Date | 9 | "date" |
The BSON Date type is a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970). Positive numbers represent the time elapsed since the epoch while negative numbers represent time moving backwards from the epoch.
Storing the date and time data as a large integer is beneficial because it:
allows MongoDB to store dates with millisecond precision
provides flexibility in how the date and time can be displayed
Because the date type does not store additional information like timezones, that context must be stored separately if it is relevant. MongoDB will store date and time information using UTC internally, but can easily convert to other timezones at time of retrieval as needed.
MongoDB also provides a that is mainly used internally:
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
Because this is mainly implemented to help coordinate internal processes like replication and sharding, you should probably not use this in your own application's logic. The date type can usually satisfy any requirements for times that you might have.
RELATED ON PRISMA.IO
When managing a MongoDB database with Prisma, the MongoDB
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type to the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 type within Prisma.
Prisma is an open-source database toolkit for Typescript and Node.js that aims to make app developers more productive and confident when working with databases.
How to create new dates
You can create a new
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object in two different ways:
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
6: Returns a date and time as a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object.
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
8: Returns a date and time as a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object.
Both the
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
6 and
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
8 methods produce a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object that is wrapped in an
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
8 helper function.
Additionally, calling
db.dates.insertMany([
{
name:"Future date",
date:ISODate("2040-10-28T23:58:18Z"),
},
{
name:"Past date",
date:newDate("1852-01-15T11:25"),
},
])
4 function without the
db.dates.insertMany([
{
name:"Future date",
date:ISODate("2040-10-28T23:58:18Z"),
},
{
name:"Past date",
date:newDate("1852-01-15T11:25"),
},
])
5 constructor returns a date and time as as string instead of a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object:
db.dates.insertMany([
{
name:"Future date",
date:ISODate("2040-10-28T23:58:18Z"),
},
{
name:"Past date",
date:newDate("1852-01-15T11:25"),
},
])
4: Returns a date and time as a string.
It is important to keep in mind this distinction between these two types as it affects what operations are available, the way the information is stored, and how much flexibility it will give you. In general, it's almost always best to store date information using the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type and then format it for output as needed.
Let's take a look at how this works in a MongoDB shell session.
First, we can switch to a new temporary database and create three documents that each have a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 field. We use a different method for populating the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 field for each object:
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
By default, each of these mechanisms will store the current date and time. You can store a different date and time by adding an ISO 8601 formatted date string as an argument:
db.dates.insertMany([
{
name:"Future date",
date:ISODate("2040-10-28T23:58:18Z"),
},
{
name:"Past date",
date:newDate("1852-01-15T11:25"),
},
])
These will create a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object at the appropriate date and time.
One thing to note is the inclusion of the trailing
db.dates.find().pretty()
2 in the first new document above. This indicates that the date and time is being provided as UTC. Specifying the date without the
db.dates.find().pretty()
2 will cause MongoDB interpret the input in relation to the current local time (though it will always convert and store it as a UTC date internally).
Validating the type of date objects
Next, we can display the resulting documents to see how MongoDB stored the date data:
db.dates.find().pretty()
{
"_id":ObjectId("62726af5a3dc7398b97e6e93"),
"name":"Created with `Date()`",
"date":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
}
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z")
}
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z")
}
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z")
}
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z")
}
As expected, the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 fields that were populated with
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
8 and
{
"acknowledged":true,
"insertedIds":[
ObjectId("62726af5a3dc7398b97e6e93"),
ObjectId("62726af5a3dc7398b97e6e94"),
ObjectId("62726af5a3dc7398b97e6e95")
]
}
6 contain
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 objects (wrapped in the
db.dates.find().pretty()
8 helper). In contrast, the field populated by the bare
db.dates.insertMany([
{
name:"Future date",
date:ISODate("2040-10-28T23:58:18Z"),
},
{
name:"Past date",
date:newDate("1852-01-15T11:25"),
},
])
4 function call is stored as a string.
You can verify that which of the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 fields contain an actual
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object by calling a
{
"_id":ObjectId("62726af5a3dc7398b97e6e93"),
"name":"Created with `Date()`",
"date":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
}
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z")
}
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z")
}
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z")
}
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z")
}
2 function over the collection. The map checks each
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 field to see if the object it stores is an instance of the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type and displays the result in a new field called
"date_storage_value":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z"),
"is_a_Date_object":true,
"date_storage_value":2235081498000
},
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z"),
"is_a_Date_object":true,
"date_storage_value":-3722502900000
}
]
This confirms that the fields displayed as
{
"_id":ObjectId("62726af5a3dc7398b97e6e93"),
"name":"Created with `Date()`",
"date":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
}
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z")
}
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z")
}
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z")
}
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z")
}
8 are instances of the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type while the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 created with the bare
db.dates.insertMany([
{
name:"Future date",
date:ISODate("2040-10-28T23:58:18Z"),
},
{
name:"Past date",
date:newDate("1852-01-15T11:25"),
},
])
4 function is not.
Additionally, the above output shows that objects stored with the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type are recorded as signed integers. As expected, the date object associated with the date from 1852 is negative because it is counting backwards from January 1970.
Querying for date objects
If you have a collection with mixed representations of dates like this, you can query for fields that have a matching type using the
For instance, to query for all of the documents where
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 is a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object, you could type:
db.dates.find({
date:{$type:"date"},
}).pretty()
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
0
To find instances where the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 field is stored as a string instead, type:
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
1
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
2
The
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type allows you to perform queries that understand the relationship between time units.
For instance, you can compare
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 objects ordinally as you would with other types. To check for future dates, you could type:
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
3
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
4
How to use use temp_db
db.dates.insertMany([ { name: "Created with `Date()`", date: Date(), }, { name: "Created with `new Date()`", date: new Date(), }, { name: "Created with `ISODate()`", date: ISODate(), },])8 type methods
You can operate on
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 objects with a variety of included methods and operators. For instance, you can extract different date and time components from a date and print in many different formats.
A demonstration is probably the quickest way to showcase this functionality.
First, let's select the date from a document with a date object:
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
5
Now, we can select the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 field and extract different components from it by calling various methods on the object:
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
6
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
7
There are also companion methods that can be used to set the time by providing different time and date components. For example, you can change the year by calling the
[
{
"_id":ObjectId("62726af5a3dc7398b97e6e93"),
"name":"Created with `Date()`",
"date":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)",
"is_a_Date_object":false,
"date_storage_value":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z"),
"is_a_Date_object":true,
"date_storage_value":2235081498000
},
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z"),
"is_a_Date_object":true,
"date_storage_value":-3722502900000
}
]
2 method:
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
8
Type | Number | Alias |
------------------ | ------ | ------------ |
Timestamp | 17 | "timestamp" |
9
We can also cast the date into different formats for display:
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
0
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
1
These are all mainly methods associated with JavaScript's
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 type.
How to use MongoDB use temp_db
db.dates.insertMany([ { name: "Created with `Date()`", date: Date(), }, { name: "Created with `new Date()`", date: new Date(), }, { name: "Created with `ISODate()`", date: ISODate(), },])8 aggregation functions
MongoDB offers some other functions that can manipulate dates as well. One useful example of this is the
[
{
"_id":ObjectId("62726af5a3dc7398b97e6e93"),
"name":"Created with `Date()`",
"date":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)",
"is_a_Date_object":false,
"date_storage_value":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z"),
"is_a_Date_object":true,
"date_storage_value":2235081498000
},
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z"),
"is_a_Date_object":true,
"date_storage_value":-3722502900000
}
]
5 aggregation function. You can pass call
[
{
"_id":ObjectId("62726af5a3dc7398b97e6e93"),
"name":"Created with `Date()`",
"date":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)",
"is_a_Date_object":false,
"date_storage_value":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z"),
"is_a_Date_object":true,
"date_storage_value":2235081498000
},
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z"),
"is_a_Date_object":true,
"date_storage_value":-3722502900000
}
]
5 with a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object, a format string specifier, and a timezone indicator. MongoDB will use the format string as a template to figure out how to output the given
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 object with the timezone being used to offset the output from UTC correctly.
Here, we will format the dates in our
[
{
"_id":ObjectId("62726af5a3dc7398b97e6e93"),
"name":"Created with `Date()`",
"date":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)",
"is_a_Date_object":false,
"date_storage_value":"Wed May 04 2022 12:00:53 GMT+0000 (UTC)"
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e94"),
"name":"Created with `new Date()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62726af5a3dc7398b97e6e95"),
"name":"Created with `ISODate()`",
"date":ISODate("2022-05-04T12:00:53.307Z"),
"is_a_Date_object":true,
"date_storage_value":1651665653307
},
{
"_id":ObjectId("62728b57a3dc7398b97e6e96"),
"name":"Future date",
"date":ISODate("2040-10-28T23:58:18Z"),
"is_a_Date_object":true,
"date_storage_value":2235081498000
},
{
"_id":ObjectId("62728c5ca3dc7398b97e6e97"),
"name":"Past date",
"date":ISODate("1852-01-15T11:25:00Z"),
"is_a_Date_object":true,
"date_storage_value":-3722502900000
}
]
9 collection using an arbitrary string. We'll also cast the dates to the New York timezone.
First, we need to remove any stray documents that might have saved the
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
7 field as a string:
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
2
Now we can run an aggregation with the
db.dates.find({
date:{$type:"date"},
}).pretty()
1 function:
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
3
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
4
The
db.dates.find({
date:{$type:"date"},
}).pretty()
2 function is similarly useful. It can be used to decompose a
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 field into its constituent parts.
For example, we can type:
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
5
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
6
The has information on additional functions you can use to manipulate
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 objects for display or comparison.
Conclusion
In this guide, we covered some of the different ways that you can work with date and time data within MongoDB. Most temporal data should probably be stored in MongoDB's
use temp_db
db.dates.insertMany([
{
name:"Created with `Date()`",
date:Date(),
},
{
name:"Created with `new Date()`",
date:newDate(),
},
{
name:"Created with `ISODate()`",
date:ISODate(),
},
])
8 data type as this provides a good deal of flexibility when operating on the data or displaying it.
Getting familiar with how date and time data is stored internally, how to coerce it into desirable formats on output, and how to compare, modify, and decompose the data into useful chunks can help you solve many different problems. While date information can be challenging to work with, taking advantage of the available methods and operators can help mitigate some of the heavy lifting.
RELATED ON PRISMA.IO
If you're using MongoDB, checkout Prisma's MongoDB connector! You can use the Prisma Client to manage production MongoDB databases with confidence.
To get started working with MongoDB and Prisma, checkout our getting started from scratch guide or how to add to an existing project.
How to compare dates in MongoDB aggregation?
How to Find Documents Between Two Dates in MongoDB. [{ "$match" : { "createddate" : { "$gt" :ISODate( "2022-01-01" ),"$lt" :ISODate( "2022-02-10" ) } }}]); The above queries return 1 record from the sample data we have added.
How to find difference between two dates in MongoDB?
The $dateDiff expression returns the integer difference between the startDate and endDate measured in the specified units . Durations are measured by counting the number of times a unit boundary is passed. For example, two dates that are 18 months apart would return 1 year difference instead of 1.5 years .
How to query greater than date in MongoDB?
MongoDB: How to Query with a Date Range - Statologyfind ( { day: { $gt: ISODate ("2020-01-21"), $lt: ISODate ("2020-01-24") } }) This particular query will return all documents in the collection where the “day” field is greater than 2020-01-21 and less than 2020-01-24.
How to get current date in MongoDB aggregation?
$currentDate sets the specified field to the date when $currentDate was run. If the field does not exist, $currentDate adds the field to a document. Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $currentDate with an empty operand expression ( { } ).