如何在Meteor中存储日期和时间进行范围查询? [英] How to store Date and Time in Meteor for range queries?

查看:137
本文介绍了如何在Meteor中存储日期和时间进行范围查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用需要创建一个在浏览器中有3个不同时区显示startDate的对象。日期也必须包括确切的时间。日期应该以这样一种方式进行存储,即允许查询给我所有的X和Y之间的日期,并解析为3个时区。

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.

我的问题是如何最好地保存(并随后检索)日期和时间,以便稍后在日期范围内查询,我是否可以使用moment.js ?我想到的是,将日期AND时间保存在数据库中的单个unix时间戳中,并且在读取时只是将其解析为具有该特定时区的日期和时间。这种方法是否正确,还是应该将其保存为纯JavaScript日期? mongoDB可以查询unix时间戳作为日期范围还是需要明确的Date对象?

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?

谢谢。

推荐答案

可以找到您需要了解的一切。这里。亮点:

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


  • 将日期作为JavaScript 日期对象存储。它们具有整数时间戳的所有查询优点,但您不需要将它们转换回 Date 对象来使用它们。

  • 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.

范围查询工作如下: Posts.find({creation_date:{$ gte:startDate,$ lt:endDate}})

您应该让服务器进行插入或使用 timesync 包以防止时钟偏移问题。

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

您可以使用 moment-timezone 格式化日期到不同的时区。例如 moment(date).tz(America / New_York)。format('l LT')

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

这篇关于如何在Meteor中存储日期和时间进行范围查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆