如何获取一整天的所有记录?
我有一些记录,我只想显示那一天,所以如果是 7 月 6 日,应该
7/6/2011 12:00:00 AM to 7/6/2011 11:59:59 AM
说我有一条记录,截止日期为 7 月 6 日晚上 7 点(我的当地时间)
07:00:00 p.m. Wednesday July 6, 2011 in Canada/Pacific converts to
02:00:00 a.m. Thursday July 7, 2011 in UTC
,即 UTC 时间凌晨 2 点。这存储在数据库中(我只存储UTC日期,然后使用UTC来获取我需要的记录,然后转换为本地时区)
现在我有这个代码
DateTime startDate = DateTime.UtcNow.Date;
DateTime endDate = DateTime.UtcNow.Date.AddSeconds(86399);
return new DateFilterRange(startDate, endDate);
// result
7/6/2011 12:00:00 AM to 7/6/2011 11:59:59 AM
当然这不会获取上述任务,因为记录存储在数据库中为 7 月 7 日,但用户仍为 7 月 6 日。
我想我必须如何使用偏移量,但我不知道如何使用它。
I have some records and I only want to show that day so if it is July 6th it should be
7/6/2011 12:00:00 AM to 7/6/2011 11:59:59 AM
Say I have a record that has a due date of July 6th 7pm (my local time)
07:00:00 p.m. Wednesday July 6, 2011 in Canada/Pacific converts to
02:00:00 a.m. Thursday July 7, 2011 in UTC
That is 2am UTC time. This is stored in the database(I store only UTC dates and then use UTC to grab records I need then convert to local time zone)
Now I have this code
DateTime startDate = DateTime.UtcNow.Date;
DateTime endDate = DateTime.UtcNow.Date.AddSeconds(86399);
return new DateFilterRange(startDate, endDate);
// result
7/6/2011 12:00:00 AM to 7/6/2011 11:59:59 AM
Of course this does not grab the above task because in the db the record is stored as July 7th but the user is still in July 6th.
I am thinking I have to use the offset some how but I am not how to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您需要获取当地时间边界(例如当地时间的开始时间),将其转换为 UTC,然后按其进行过滤。
请注意,您的初始示例仅显示 7 月 6 日的上半月 - 上午 12 点到下午 12 点。目前尚不清楚这是否是您想要的。
请注意,在某些时区,并非所有日子都从午夜开始 - 如果在午夜发生时发生夏令时更改,则这一天实际上可能从凌晨 1 点开始。
编辑:要明确的是...
您需要以某种方式从用户那里获取年、月和日。如果在 UI 中指定了这一点,则可以使用以下代码:
否则,您将需要类似以下内容的代码:
请注意,此不考虑到午夜并不总是发生的时区。
It sounds like you need to take your local time boundaries, e.g. the start of the local day, convert those to UTC, and then filter by that.
Note that your initial example only shows the first half of July 6th - 12am to 12pm. It's not clear whether that's what you want.
Be aware that in some time zones, not all days start at midnight - if a DST change occurs when midnight would occur, the day may actually start at 1am.
EDIT: Just to be clear...
You need to get year, month and day from the user somehow. If that's specified in the UI, you can use this code:
Otherwise, you'll want something like:
Note that this doesn't take into account time zones where midnight doesn't always happen.
注意:我假设您根据评论已经知道用户时区...
1 得到“现在”。
2 转换为用户的时区。
3 获取用户所在时区的日期边界。
4 将边界转换为 UTC 时间。
当然,.Date 和 .AddDays() 操作发生在本地时区,因此在夏令时等方面它不会是完美的。
Note: I'm assuming you already know the users time zone, based on comments...
1 get "right now".
2 convert to user's time zone.
3 get day boundaries in user's time zone.
4 convert boundaries to UTC time.
Of course, the .Date and .AddDays() operations happen in the local time zone, so it won't be perfect with regard to daylight savings time etc.