Model Time Data

Overview

MongoDB stores times in UTC by default, and will convertany local time representations into this form. Applications that must operate orreport on some unmodified local time value may store the time zone alongside theUTC timestamp, and compute the original local time in their application logic.

Example

In the MongoDB shell, you can store both the current date and the currentclient’s offset from UTC.

  1. var now = new Date();
  2. db.data.save( { date: now,
  3. offset: now.getTimezoneOffset() } );

You can reconstruct the original local time by applying the saved offset:

  1. var record = db.data.findOne();
  2. var localNow = new Date( record.date.getTime() - ( record.offset * 60000 ) );