角度材料的日期选择器日期错误 [英] Wrong date with angular material's date picker

查看:24
本文介绍了角度材料的日期选择器日期错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用日期选择器选择一个日期并将其发送到服务器.

I use the datepicker to pick a date and send it to the server.

当我记录 JS 值时,我得到了正确的结果:

When I log the JS value I get the correct result:

Tue Mar 22 2016 00:00:00 GMT+0100 (Mitteleuropäische Zeit)

但在ajax请求中是

2016-03-21T23:00:00.000Z

我不修改值,只是将对象提供给 angulars http 函数.

I don't modify the values, just giving the object to angulars http function.

Angular 是否需要一些配置来处理它?<​​/p>

Does Angular need some configuration to handle it?

推荐答案

这两个字符串代表同一时间.一种是 UTC,即 GMT +0,您可以从 Z 结尾看到.另一个位于不同的时区,特别是格林威治标准时间 +1 小时.

Those two strings represent the same time. One is in UTC, i.e. GMT +0, which you can see from the Z ending. The other is in a different timezone, specifically GMT +1 hour.

如果您有两个字符串的 javascript 日期对象,并将它们转换为整数,即自 1970 年 1 月 1 日(UTC)以来经过的秒数,您会发现它们是相同的.它们代表相同的即时,但位于两个不同的地理位置.

If you had javascript date objects for both strings, and turned them into integers, i.e. seconds passed since Jan 1, 1970, UTC, you'd find them identical. They represent the same instant but in two different geographic locations.

var d1 = new Date('Tue Mar 22 2016 00:00:00 GMT+0100');
var d2 = new Date('2016-03-21T23:00:00.000Z');

Number(d1);  // 1458601200000
Number(d2);  // 1458601200000

总的来说,这是一件好事.处理时区变得非常混乱.我发现服务器最好始终使用 UTC.

Generally this is a good thing. Dealing in timezones gets very confusing. I find it best for a server to always deal in UTC.

这篇关于角度材料的日期选择器日期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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