Store date in MongoDB without considering the timezone
Whole dates should not be placed in a Date
object. Despite the name, Date
represents a date and a time. If the value you're working with represents a whole date, rather than a specific time on that date, then you should store the value in another format.
Options are:
- A string in
YYYY-MM-DD
orYYYYMMDD
format - An integer number of whole days since some reference point.
This means "you did it wrong". Good database practice has long been to store dates in UTC or Zulu time format. So what you should have done ( at least in JavaScript notation ) is this:
var myDate = new Date ("2014-12-15");
And you should have considered that the date string you fed in was already converted from your local time to a UTC time, or just feed your object anyway since that is what the driver will do.
JavaScript has funny ways of interpreting the date "string" provided. So for example:
var myDate = new Date ("2014/12/15");
Actually provides a date object in your "local" timezone to your application. Probably not what you really want in most circumstances.
Best Practice. Cannot emphasize that enough. The "universal time constant" is the only way you ever store date data. Otherwise you are doing it wrong.