Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Introducing MongoDB 8.0, the fastest MongoDB ever!
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Quick Start: BSON Data Types - Date

Ken W. Alger2 min read • Published Jan 31, 2022 • Updated Sep 23, 2022
MongoDB
Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
BSON Quickstart badge
Dates and times in programming can be a challenge. Which Time Zone is the event happening in? What date format is being used? Is it MM/DD/YYYY or DD/MM/YYYY? Settling on a standard is important for data storage and then again when displaying the date and time. The recommended way to store dates in MongoDB is to use the BSON Date data type.
The BSON Specification refers to the Date type as the UTC datetime and is a 64-bit integer. It represents the number of milliseconds since the Unix epoch, which was 00:00:00 UTC on 1 January 1970. This provides a lot of flexibilty in past and future dates. With a 64-bit integer in use, we are able to represent dates roughly 290 million years before and after the epoch. As a signed 64-bit integer we are able to represent dates prior to 1 Jan 1970 with a negative number and positive numbers represent dates after 1 Jan 1970.

Why & Where to Use

You'll want to use the Date data type whenever you need to store date and/or time values in MongoDB. You may have seen a timestamp data type as well and thought "Oh, that's what I need." However, the timestamp data type should be left for internal usage in MongoDB. The Date type is the data type we'll want to use for application development.

How to Use

There are some benefits to using the Date data type in that it comes with some handy features and methods. Need to assign a Date type to a variable? We have you covered there:
1var newDate = new Date();
What did that create exactly?
1> newDate;
2ISODate("2020-05-11T20:14:14.796Z")
Very nice, we have a date and time wrapped as an ISODate. If we need that printed in a string format, we can use the toString() method.
1> newDate.toString();
2Mon May 11 2020 13:14:14 GMT-0700 (Pacific Daylight Time)

Wrap Up

Get started exploring BSON types, like Date, with MongoDB Atlas today!
The date field is the recommended data type to use when you want to store date and time information in MongoDB. It provides the flexibility to store date and time values in a consistent format that can easily be stored and retrieved by your application. Give the BSON Date data type a try for your applications.

Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Code Example

Example Application for Dog Care Providers (DCP)


Jul 12, 2024 | 3 min read
Quickstart

Introduction to the MongoDB Aggregation Framework


Sep 23, 2022 | 5 min read
Article

Using MongoDB With Rust Web Development Framework


Aug 29, 2024 | 1 min read
Tutorial

A Beginner's Guide to Integrating MongoDB With TensorFlow Using JavaScript


Sep 04, 2024 | 15 min read
Table of Contents
  • Why & Where to Use