Storing Dates with timezone offset

463 views
Skip to first unread message

Matthew Hillsborough

unread,
Mar 19, 2014, 1:33:20 PM3/19/14
to mon...@googlegroups.com
Hi all,

I am interested in getting a timezone offset in my MongoDB Date field. Using MongoDB 2.4.9 on one machine, I see the following which is the correct way the Date should be stored:

    NOVUSNYCLK67(mongod-2.4.9) test_salat> db.scala_date_test_1.find().toArray()
    [
      {
        "_id": ObjectId("5329bf52e4b089adba4e75ec"),
        "_typeHint": "com.novus.salat.test.model.Neville",
        "ennui": true,
        "asOf": ISODate("2014-03-19T12:01:22.355-0400")
      }
    ]

Note the "-0400" fragment of the ISODate() here. On another machine, it looks like the following (wrong way):

    > db.scala_date_test_1.find().toArray()
    [
    {
    "_id" : ObjectId("5329c755c026b6d16dc5972b"),
    "_typeHint" : "com.novus.salat.test.model.Neville",
    "ennui" : true,
    "asOf" : ISODate("2014-03-19T16:35:33.474Z")
    }
    ]

I'm using Mongoid to persist these dates, but in reality, I just want to know how to get the timezone offset in my Dates so they are formatted like the following:

    "asOf": ISODate("2014-03-19T12:01:22.355-0400")

and NOT like:

    "asOf" : ISODate("2014-03-19T16:35:33.474Z")

Thank you for the help.

Frederick Cheung

unread,
Mar 20, 2014, 6:07:26 AM3/20/14
to Matthew Hillsborough, mon...@googlegroups.com



On 19 March 2014 at 17:33:23, Matthew Hillsborough (matthew.hi...@gmail.com) wrote:
> Hi all,
>
> I am interested in getting a timezone offset in my MongoDB Date field.
> Using MongoDB 2.4.9 on one machine, I see the following which is the
> correct way the Date should be stored:


I think this is just a display issue - dates are never stored with a timezone in mongo - they're just an number of milliseconds from the epoch

For whatever reasons one of your machines thinks that your local timezone is -0400 and so formats the dates accordingly, and the other thinks your local timezone is UTC

Fred

 
Reply all
Reply to author
Forward
0 new messages