MongoDB - Storing date without timezone

后端 未结 2 1447
北海茫月
北海茫月 2021-01-18 16:01

We have a simple application in which we have all user in same timezone & therefore we are not interested to store timezone information in mongo date object.

Re

相关标签:
2条回答
  • 2021-01-18 16:52

    Look at this answer: https://stackoverflow.com/a/6776273/6105830

    You can use two types of long representation (milliseconds or format yyyyMMddHHmmss). These are the only ways to not store timezone and still be able to make range queries.

    Unfortunately you lost some aggregation properties. But you can do something like keeping two representations and use them at opportune times.

    UPDATE:

    Do not store date as I said before. You will lost many and many features of MongoDB and also will be hard to perform major operators on date fields.

    Newer versions of MongoDB has operators to deal with timezone, and it should be enough to work with ISOTime formats. My application was using my own suggestion to store date. Now I have to let my users select their TimeZone (company has grown and we need to expand to other countries). We are struggling to change all models to use timestamp instead of a normalized date format. For further more explore the link: https://docs.mongodb.com/manual/reference/method/Date/

    and you can also use MongoDB official community channel for questioning Here is the link: https://developer.mongodb.com/community/forums/

    0 讨论(0)
  • 2021-01-18 17:03

    You could consider storing all the dates in UTC and presenting to the users in UTC, so you don't have the problem of silent conversion by either client JavaScript, server or MongoDB and therefore confusion. You can do it like this: new Date(Date.UTC(2000, 01, 28)) Here's MDN link on the subject.

    0 讨论(0)
提交回复
热议问题