0

I have a date string of the form: "Fri Jun 20 16:14:00 GMT+0530 2014" As you can see there is TimeZone information in the date (GMT+0530).

But once we store this in MongoDB using mongodb 'date' datatype. I see it is stored in a format like this:'2014-06-20 10:44:00.470Z'

How can i extract the 'original' Time Zone from this date??
(I am using Java for storing/extracting data from mongo)

Jasper
  • 8,440
  • 31
  • 92
  • 133
  • please visit this link http://stackoverflow.com/questions/20800895/how-to-insert-the-same-date-in-mongodb-as-read-from-string-through-java – Keval Trivedi Jun 24 '14 at 12:16

2 Answers2

2

The BSON Date data type that MongoDB uses is simply a 64-bit integer count of milliseconds since UTC Jan 1, 1970. So if you want to track a time stamp's original time zone you'd have to store that separately as it's lost when converted to Date.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • JohnnyHK> While converting to UTC - would the source datetime's TimeZone be taken into consideration? For eg consider these: 03-July-2014 14:25:00 EDT, 03-July-2014 14:25:00 PST, 03-July-2014 14:25:00 IST (only TZ is varying) - would all of these convert to exact same UTC, or different UTC values? – Jasper Jun 27 '14 at 08:19
  • @Jasper Those would all convert to different UTC values according to the UTC-offset of each time zone. – JohnnyHK Jun 30 '14 at 04:07
0

MongoDB stores dates in ISO 8601 format. There's no straight forward way to convert to it from the Java date format, but here's a similar question, look at the accepted answer, it should give you a clue on how to do this.

Community
  • 1
  • 1
ethanfar
  • 3,733
  • 24
  • 43
  • @JohnnyHK see http://stackoverflow.com/questions/17195754/how-to-convert-date-format-in-mongodb – ethanfar Jun 24 '14 at 12:26
  • 1
    That one's wrong too. :-) [`ISODate`](http://docs.mongodb.org/manual/core/shell-types/#date) is just a constructor function in the MongoDB shell to help converting ISO 8601 strings to native `Date`. – JohnnyHK Jun 24 '14 at 12:38
  • In either case, the string he's seeing stored in MongoDB is absolutely in the ISO 8601 format. Even if you're right about MongoDB not storing it in this format, that's the end result for him anyway. That's why he should refer to the answered question I linked in my answer, which should solve his problem. – ethanfar Jun 24 '14 at 12:42
  • The point is that it's not stored as a string in MongoDB. What the OP is seeing is simply how it is displayed in whatever tool they're using to query it (e.g. the MongoDb shell). – JohnnyHK Jun 24 '14 at 12:56
  • Currently MongoDB will try to strip tzs down to utz, there are ways to stop it from doing that though I won't mention them here because I believe the database should never store tz, the tz should be viewing client location dependant. – Sammaye Jun 24 '14 at 14:12