0

in Mongodocs, it is said that datetimes with specified "Z" timezone at the end are saved as "UTC" datetime format.. https://docs.mongodb.com/manual/reference/method/Date/

I created some sample time data in Python:

now=str((datetime.datetime.now()).isoformat())+'Z'  

then=str((datetime.datetime.utcnow()+datetime.timedelta(0,one_week_in_seconds)).isoformat())+'Z'

I used datetime.now() and datetime.utcnow() and appended 'Z' on both... this is what I get:

'now': '2018-07-10T11:06:05.512484Z',
'then': '2018-07-17T09:06:05.512484Z',

I am now using MEAN stack with Node/Express and mongoose (ODM) Driver to make my schema models in the database. When I push the data via some router middleware to my mongoDB Database, the two fields have mongoose "Date" format. However, for both fields it creates an ISODate time format...:

 "now" : ISODate("2018-07-10T09:02:01.410Z"),
 "then" : ISODate("2018-07-17T09:02:01.410Z"),

I think thats a bug, normally, if "Z" is specified, it should create the specified time in ISO-Format, which it is here, but as I have created the time in local-time format and appended a "Z" in the first case ("now"), the time should be saved as 'now' : ISODate("2018-07-10T11:06:05.512484Z") without modifying / converting from local to UTC time or not?

MMMM
  • 3,320
  • 8
  • 43
  • 80
  • Not quite sure what you're asking, but all [datetimes stored in MongoDB](https://docs.mongodb.com/manual/reference/bson-types/#date) are in UTC as a 64-bit integer millisecond count. The `ISODate` construct that you see in the shell and the string with a Z suffix is just a shell representation of that UTC datetime. – JohnnyHK Jul 10 '18 at 13:40
  • my original question (or what I was trying to point out) was, that datetime objects with timezone "Z" suffix should be saved "as is" (means local time) from client, without being converted to some UTC Datetime, or not? If I give the "Z", I want to express that its already UTC time and should be saved "as is". But it gets converted to UTC with some offset still..? why? – MMMM Jul 10 '18 at 14:48
  • Can you update your question with a reproducible example that's not working as you expect? – JohnnyHK Jul 10 '18 at 14:50
  • not sure how to give you a reproducible example, unless you have Node/Express with routing links as backend, and can do a post to your middleware, and you have declared Date field on your schema, and you create a datetime in Python and send it with requests library to your Post Url to create the dataset .. ;) – MMMM Jul 10 '18 at 15:00
  • The idea would be to simplify it down to something you can reproduce in the JavaScript MongoDB shell. – JohnnyHK Jul 10 '18 at 15:22

1 Answers1

0

if you want to save specific country timezone and if you are using moment then it is easy to manage

below link will help to you

Updating time offset with moment().utcOffset()

  • thanks but thats not what I wanted to say... I mean MongoDB itself stores Datetime values with "Z" timezone "AS IS" (which means, the local time) in mongodb normally (as far as I understood from their docs..) but mongoose or whatever creates the Datetime with "Z" timezone, is still converting my time into UTC time, and that shouldn't be I think..? – MMMM Jul 10 '18 at 14:46