我有一个网页,其中包含日、月和年的三个下拉菜单.如果我使用接受数字的 JavaScript Date
构造函数,那么我将获得当前时区的 Date
对象:
I have a web page with three dropdowns for day, month and year. If I use the JavaScript Date
constructor that takes numbers, then I get a Date
object for my current timezone:
new Date(xiYear, xiMonth, xiDate)
给出正确的日期,但由于夏令时,它认为日期是 GMT+01:00.
Give the correct date, but it thinks that date is GMT+01:00 due to daylight savings time.
这里的问题是,然后我将此 Date
传递给 Ajax 方法,当日期在服务器上被反序列化时,它已被转换为 GMT,因此丢失了一个小时,从而将日期向后移动一.现在我可以将日、月和年单独传递到 Ajax 方法中,但似乎应该有更好的方法.
The problem here is that I then pass this Date
to an Ajax method and when the date is deserialised on the server it has been converted to GMT and so lost an hour which moves the day back by one.
Now I could just pass the day, month, and year individually into the Ajax method, but it seems that there ought to be a better way.
接受的答案为我指明了正确的方向,但是仅使用 setUTCHours()
本身就发生了变化:
The accepted answer pointed me in the right direction, however just using setUTCHours()
by itself changed:
Apr 5th 00:00 GMT+01:00
到
Apr 4th 23:00 GMT+01:00
然后我还必须设置 UTC 日期、月份和年份以结束
I then also had to set the UTC date, month and year to end up with
Apr 5th 01:00 GMT+01:00
这正是我想要的.
使用 .setUTCHours()
可以在 UTC-time 中实际设置日期,这将允许您使用 UTC-整个系统的时间.
using .setUTCHours()
it would be possible to actually set dates in UTC-time, which would allow you to use UTC-times throughout the system.
你不能在构造函数中使用 UTC 设置它,除非你指定一个日期字符串.
使用 new Date(Date.UTC(year, month, day, hour, minute, second))
您可以从特定的 UTC 时间创建 Date-object.
Using new Date(Date.UTC(year, month, day, hour, minute, second))
you can create a Date-object from a specific UTC time.
这篇关于创建具有设定时区的日期而不使用字符串表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!