Javascript Date() 给出错误的日期一小时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32469269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Date() give wrong date off by one hour
提问by Youssef
I send this date from my controller in java (Spring-MVC
) the type in mysql
is datetime
我从我的控制器在 java ( Spring-MVC
) 中发送这个日期,类型mysql
是datetime
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "..") public Date getYy() {
return this.yy;
}
as : [2015-09-30 00:00:00.0]
作为 : [2015-09-30 00:00:00.0]
When i get this dates with ajax as 1443567600000
:
当我用 ajax 得到这个日期时1443567600000
:
new Date(1443567600000)
convert to Tue Sep 29 2015 23:00:00 GMT+0000 (Maroc)
new Date(1443567600000)
转换成 Tue Sep 29 2015 23:00:00 GMT+0000 (Maroc)
So why i get wrong date off by one hour?
那么为什么我的日期错了一小时呢?
SOLUTION
解决方案
We resolve it by
我们通过以下方式解决
d = new Date(value) ;
d.setTime( d.getTime() - new Date().getTimezoneOffset()*60*1000 );
because it was Daylight saving time (DST) or summer time problem. good article
因为这是夏令时 (DST) 或夏令时问题。好文章
采纳答案by Crazy Crab
I think maybe this is a Daylight Saving Timeproblem. You can check your client's timezone, and your server's timezone. (web server or SQL Server)
我想这可能是夏令时问题。您可以检查客户端的时区和服务器的时区。(Web 服务器或 SQL Server)
回答by ZER0
We should probably need more data about it, but it could be that nothing is wrong here, it depends how you set and get back your date.
我们可能需要更多关于它的数据,但可能这里没有任何问题,这取决于您如何设置和取回日期。
Basically 1443567600000
doesn't contains timezone. It represent Tue Sep 29 2015 23:00:00
from Greenwich. It's a moment in time that, of course, it different from any location that has a different timezone. The same moment, happens at different time (the midnight of GMT+1 is the 11pm of GMT).
基本上1443567600000
不包含时区。它代表Tue Sep 29 2015 23:00:00
来自格林威治。当然,这是一个时刻,它不同于任何具有不同时区的位置。同一时刻,发生在不同的时间(GMT+1 的午夜是 GMT 的晚上 11 点)。
You have to store both the time and the timezone in your DB, and possibly send back to JS, otherwise it will be always interpreted differently depends by the local time of the client.
您必须将时间和时区都存储在您的数据库中,并可能发送回 JS,否则它将始终根据客户端的本地时间进行不同的解释。
To make an example:
举个例子:
var d = new Date(2015, 8, 30);
console.log(d.toJSON()); // In my case I got "2015-09-29T22:00:00.000Z"
console.log(d.toDateString()); // "Wed Sep 30 2015"
回答by mike rodent
This JS handling of Date is a quite a head-flip.
这个 JS 对 Date 的处理是一个相当大的翻转。
I'm in the UK... "easy" because we're on GMT (UTC)... except during the summer months, when there's DST (British Summer Time, BST). Clocks go forward in summer and back in winter (stupidly by the way, but that's another issue!) by one hour. One day in March what is 4pm GMT is now called5pm (BST).
我在英国......“容易”,因为我们在格林威治标准时间(UTC)......除了夏季月份,当时有 DST(英国夏令时,BST)。时钟在夏天前进,冬天倒退(顺便说一句,这是另一个问题!)一小时。三月的某一天格林威治标准时间下午 4 点现在称为下午 5 点(英国夏令时)。
summermonth:
夏季月份:
If you do new Date( '2017-08-08' )
this will give you (toString
) 'Date 2017-08-08T00:00:00.000Z'.
如果你这样做,new Date( '2017-08-08' )
会给你 ( toString
) 'Date 2017-08-08T00:00:00.000Z'。
If you do new Date( '2017-08-08 00:00' )
, however, this will give you 'Date 2017-08-07T23:00:00.000Z'!
new Date( '2017-08-08 00:00' )
但是,如果您这样做,这将为您提供“日期2017-08-07T23:00:00.000Z”!
In the second case it appears JS is trying to be "helpful" by assuming that because you stipulated the hour you were specifying BST time. So it adjusts to GMT/UTC. Otherwise it doesn't... though (of course) it still produces a Date
object which is specific right down to the milliseconds. Quite a gotcha!
在第二种情况下,JS 似乎试图通过假设来“有帮助”,因为您规定了指定 BST 时间的小时。所以它会调整为 GMT/UTC。否则它不会......尽管(当然)它仍然会产生一个Date
特定于毫秒的对象。相当有问题!
Confirmation: a wintermonth... when BST is not applied:
new Date( '2018-01-01 00:00' )
/ new Date( '2018-01-01' )
: both give 'Date 2018-01-01T00:00:00.000Z'
确认:一个冬天的月份......当不应用 BST 时:
new Date( '2018-01-01 00:00' )
/ new Date( '2018-01-01' )
:两者都给出“日期 2018-01-01T00:00:00.000Z”
As for adjusting, it appears that to undo the automatic adjustment you just go
至于调整,看来要取消自动调整就行了
jsDate.setTime( jsDate.getTime() + jsDate.getTimezoneOffset() * 60 * 1000 );
... this is a bit like what Youssef has written... except you have to obtain the offset from the specific Date
in question... and my experiments seem to prove that you add this, not subtract it.
...这有点像 Youssef 所写的......除了你必须从所讨论的特定对象Date
中获得偏移量......我的实验似乎证明你添加了它,而不是减去它。
回答by Daniel Santos
To be more specific
更具体
time = new Date("2018-06-01 " + time);
var offset = time.getTimezoneOffset();
offset = Math.abs(offset / 60);
time.setHours(time.getHours() + offset);
in this case time is equal to hours with leading zeros:minutes with leading zeros. This will add the hours difference to the UTC. If you pass a string to date it is treated as UTC.
在这种情况下,时间等于带前导零的小时:带前导零的分钟。这会将小时差添加到 UTC。如果您将字符串传递给日期,则将其视为 UTC。