问题
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
Dateobjects. They have all the query advantages of integer timestamps, but you don't need to convert them back toDateobjects 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