当只需要保存日期时,将其转换为UTC然后转换为本地可能在不同时区中表现不同 [英] When date only is required to save, Converting it to UTC and then to local may behave differently in different time zones

查看:38
本文介绍了当只需要保存日期时,将其转换为UTC然后转换为本地可能在不同时区中表现不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在前端有一个日期字段.我正在从一个说19/04/2018的时区保存它,而我的电话是+8.当我在本地datetime上加载它时,ToLocalTime()可以在+8 offset中完美运行,它将显示19/04/2018,但坐在+7的人将在18/04/2018 23:00进入,因此它将显示182018年4月4日.如何处理这种情况.

I have a date field at front end. And I am saving it from a time zone say 19/04/2018 and I am on +8. When I load it on local datetime.ToLocalTime() works perfectly in +8 offset and it will show 19/04/2018 but a person sitting in +7 would get in 18/04/2018 23:00 and hence it will show 18/04/2018. How to handle this case.

推荐答案

通常:您不应仅将日期转换为UTC,因此应将其保留为本地日期.

In general: you should not convert date only to UTC, so you should keep it as local date.

但这确实取决于数据的使用.

But it really depend on the use of data.

如果您要进行一些同步(所有发票直到固定点),则带时区的时间比较合适,但是在这种情况下,还应该包括小时和分钟.

If you are using for some synchronization (all invoices until a fix point), a time with timezone is good, but in such case, also hours and minutes should be included.

如果您关心发票上写的日期,则不能将日期转换为UTC(因此请保留本地日期).如果您汇总发票,则可以等到所有时区的日期过去(并且人们将所有发票填入系统).

If you care about the date written in an invoice, you may not transform date to UTC (so keep local date). If you aggregate invoices, you may wait until the day is passed on all timezone (and people filled all invoices into the system).

当人们调查数据时,他们期望什么?(案例1或案例2).然后评估您需要哪种数据.在第一种情况下,您还应该(至少)添加小时和分钟(时区永远不会有秒).

When people are looking into the data, what they expect? (case #1 or case #2). Then evaluate what kind of data you need. In the first case, you should add also (at minimum) the hours and minute (timezones never have seconds).

通常,如果只有日期,则大多数情况下是第二种情况.

In general, if you have only a date, you are mostly on second case.

这篇关于当只需要保存日期时,将其转换为UTC然后转换为本地可能在不同时区中表现不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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