Angular JS-提交到$ http时的日期更改-时区问题 [英] Angular JS - Date changes when submitting to $http - Timezone issue
问题描述
我遇到一个奇怪的问题,当Date
通过$ http.put传递给API时,它会发生变化,我怀疑是时区问题:
I am having a strange problem where a Date
changes when it is passed to API through $http.put, I suspect a timezone issue:
Datepicker触发ng-change事件-console.log:
Datepicker triggers ng-change event - console.log:
Tue Jun 10 2014 00:00:00 GMT+0100 (GMT Summer Time)
使用Angular $ http.put传递给API ...
Passed to API using Angular $http.put...
击中提琴手时:
StartDate=2014-06-09T23:00:00.000Z
您会看到日期从6月10日更改为6月9日.
As you can see the date changes from 10th June to 9th June.
如何停止日期更改?是引起更改的时区吗? API和客户端都在Localhost上运行.
How can I stop this change of date? Is it the timezone causing the change? Both the API and client side are running on Localhost.
进一步:
再次单击该字段并启动日期选择器/选择日期时,第二次没有出现问题:
When the field is clicked a second time and the datepicker launched / date selected, this second time the problem does not appear:
Console.log:
Console.log:
Wed Aug 06 2014 22:00:00 GMT+0100 (GMT Summer Time)
收到提琴手数据:
StartDate=2014-08-06T21:00:00.000Z
推荐答案
我认为这是一个TZ问题,因为GMT+0100
与StartDate=2014-06-09T23:00:00.000Z
之间的区别是 5小时
I think it is a TZ issue, b/c the difference between your GMT+0100
and your StartDate=2014-06-09T23:00:00.000Z
is 5 hours.
.
您当前的本地时间是 BST(英国夏令时),相当于 GMT +1
Your local time is currently BST (British Summer Time) equivalent to GMT +1
这将是您进行API调用的默认时间.
This is going to be the default time when you make your API call.
但是,该API是由Google在加利福尼亚编写的,它们以自我为中心.因此,由于您没有提供任何date
格式的说明" ,它们会自动转换时间.
However, the API was written by Google in California, and they are rather egocentric. Therefore, they're automatically converting the time since you're not providing any date
formatting "instructions".
换句话说,当您打印到console.log()
时,Chrome会为您很好地进行转换,但是当您为$http.put
, Angular 进行设置时,却没有为您提供明确的格式设置,就会进行调用使用默认的TZ(PST
)
In other words, Chrome does the conversion for you nicely when you print to the console.log()
, but when you make your $http.put
, Angular, without explicit formatting for you, makes the call using it's default TZ (which is PST
)
.
您需要将日期格式强制设置为您的语言环境.
You need to force the date formatting to your locale.
-
角度模板
Angular template
{{ date_expression | date : 'yyyy-MM-dd HH:mm:ss'}}
在JavaScript中
In JavaScript
$filter('date')(date, 'yyyy-MM-dd HH:mm:ss')
使用本地化
using localization
<html>
<head>
<title>My Angular page</title>
<script src="angular-locale_en-uk.js"></script>
...
</head>
<body>
...
</body>
</html>
这篇关于Angular JS-提交到$ http时的日期更改-时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!