How to store Date and Time in Meteor for range queries?

空扰寡人 提交于 2019-12-19 04:04:12

问题


My app needs to create objects that will have a startDate shown in 3 different timezones in browser. The date must also include the exact time. The date should be stored in such a way that it allows for queries "give me all for date between X and Y" and parse it to 3 timezones.

My question is how should I best save (and subsequently retrieve) the date and time so that I can query it later in date ranges, should I maybe use moment.js? What I thought of, is saving the date AND time both in a single unix timestamp in the database, and when reading it just parse it into the date and time with that specific timezone. Is this approach correct, or should I save it as plain javascript Date? Can mongoDB query unix timestamps as date ranges or does it need plain Date objects?

Thanks.


回答1:


Everything you need to know about this can be found here. The highlights:

  • Store dates as JavaScript Date objects. They have all the query advantages of integer timestamps, but you don't need to convert them back to Date objects to use them.

  • Range queries work like so: Posts.find({creation_date: {$gte: startDate, $lt: endDate}})

  • You should either have the server do the inserts or use the timesync package to prevent clock skew problems.

  • You can use moment-timezone to format you dates to different timezones. e.g. moment(date).tz("America/New_York").format('l LT')



来源:https://stackoverflow.com/questions/25393603/how-to-store-date-and-time-in-meteor-for-range-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!