I've seen some using strings, int timestamps and some mongo datetime objects.
A:
The best way is to store native JavaScript Date objects. It supports a whole range of useful methods out of the box, which you can use in your map-reduce jobs, for example.
If you need to, you can easily convert Date objects to and from Unix timestamps1), using the getTime()
method and Date(milliseconds)
constructor respectively.
1) Strictly speaking, the Unix timestamp is measured in seconds. The JavaScript Date object measures in milliseconds since the Unix epoch.
Niels van der Rest
2010-09-23 13:28:14
How will that be stored in the DB? As a mongo datetime object?
Thilo
2010-09-24 00:45:09
@Thilo: MongoDB has no special 'datetime' object as far as I know. It uses the JavaScript Date type, which is stored in BSON form.
Niels van der Rest
2010-09-24 08:04:01
http://bsonspec.org/#/specification lists a "UTC datetime" data type.
Thilo
2010-09-25 02:37:13
@Thilo: Correct, that is basically the BSON representation of a JavaScript Date object. It's a 64-bit integer that stores the milliseconds since the Unix epoch and supports (most?) of the methods from the [JavaScript specification](http://w3schools.com/jsref/jsref_obj_date.asp).
Niels van der Rest
2010-09-25 06:16:04