Navigation
This version has reached end of life on October 2016. Learn more about upgrading your version of MongoDB.

Date()

On this page

Date()

Returns a date either as a string or as a Date object.

  • Date() returns the current date as a string.
  • new Date() returns the current date as a Date object. The mongo shell wraps the Date object with the ISODate helper.
  • new Date("<YYYY-mm-dd>") returns the specified date string ("<YYYY-mm-dd>") as a Date object. The mongo shell wraps the Date object with the ISODate helper.

Behavior

Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.

Examples

Return Date as a String

To return the date as a string, use the Date() method, as in the following example:

var myDateString = Date();

Return Date as Date Object

The mongo shell wrap objects of Date type with the ISODate helper; however, the objects remain of type Date.

The following example uses new Date() to return Date objects.

var myDate = new Date();