When date only is required to save, Converting it to UTC and then to local may behave differently in different time zones

假如想象 提交于 2021-02-10 19:52:54

问题


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.


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/49921991/when-date-only-is-required-to-save-converting-it-to-utc-and-then-to-local-may-b

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