Angular UI DatePicker 调整时区 [英] Angular UI DatePicker adjusting for timezone

查看:23
本文介绍了Angular UI DatePicker 调整时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将日期存储为 SQL Server 中的日期.当我在 SQL 中查询时,日期显示 4/24/2014.那是正确的.日期以 UTC 格式正确传送到客户端.为了编辑那个日期,我们使用了 Angular UI DatePicker.DatePicker 正在根据我的本地时区调整该日期,因此它总是关闭一天.

我可以看到它正在发生.如果我们编辑的是 DateTime 而不是 Date,那么调整时区是正确的.但是,在这种情况下,我只有一个日期,所以我不关心时区,我只想编辑数据库中的日期.

我可以验证它是否针对时区进行了调整.如果我将 Windows 机器上的时区更改为 UTC,则 DatePicker 确实会显示正确的日期.

那么,问题是,有没有办法告诉 DatePicker 关闭时区调整并只管理 UTC 格式的日期,以便它可以使用 SQL 日期而不是 SQL 日期时间?

解决方案

不久前我遇到了一个非常相似的问题:我想在服务器端存储本地日期(即只有 yyyy-mm-dd 而没有 timezome/time信息),但由于 Angular Bootstrap Datepicker 使用 JavaScript Date 对象,这是不可能的(如您自己发现的那样,它会序列化为 JSON 中的 UTC 日期时间字符串).

我用这个指令解决了这个问题:https://gist.github.com/weberste/354a3f0a9ea58e0ea0de>

本质上,每当在日期选择器上选择日期时,我都会重新格式化该值(该值,一个 yyyy-mm-dd 格式的字符串,将存储在模型中)以及每当访问模型以填充视图时,我需要再次将它包装在 Date 对象中,以便 datepicker 正确处理它.

I have a date stored as a Date in SQL Server. The date shows 4/24/2014 when I query in SQL. That is correct. The date is correctly brought over to the client side in UTC. To edit that date we're using the Angular UI DatePicker. The DatePicker is adjusting that date based on my local timezone and so it is always off by one day.

I can see that it's happening. If we were editing a DateTime instead of a Date, then it would be correct to adjust for the timezone. However, in this case, I just have a Date, so I don't care about timezone and I just want to edit the date as it was in the database.

I can verify that it's adjusting for timezone. If I change the timezone on my windows machine to be UTC then the DatePicker does show the correct date.

So, the question is, is there a way to tell the DatePicker to turn off the timezone adjustments and just manage dates in UTC format so that it will work with a SQL Date instead of a SQL Datetime?

解决方案

I had a very similar problem a while ago: I wanted to store local dates on the server side (i.e. just yyyy-mm-dd and no timezome/time information) but since the Angular Bootstrap Datepicker uses the JavaScript Date object this was not possible (it serializes to a UTC datetime string in the JSON as you found out yourself).

I solved the problem with this directive: https://gist.github.com/weberste/354a3f0a9ea58e0ea0de

Essentially, I'm reformatting the value whenever a date is selected on the datepicker (this value, a yyyy-mm-dd formatted string, will be stored on the model) and whenever the model is accessed to populate the view, I need to wrap it in a Date object again so datepicker handles it properly.

这篇关于Angular UI DatePicker 调整时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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