角度$ http.post将日期更改为UTC日期 [英] angular $http.post changing date to UTC date

查看:38
本文介绍了角度$ http.post将日期更改为UTC日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些数据发布到具有日期的REST api中.现在,当我调试时,我的date参数是在我的时区中具有正确日期的JS Date对象: Tue Apr 04 2017 00:00:00 GMT + 0530

I was trying to post some data to my REST api which has date. Now while I debug, my date parameter is a JS Date object with correct date in my timezone: Tue Apr 04 2017 00:00:00 GMT+0530

它离开我的代码后,并且在网络标签中看到相同的内容,它转换为UTC日期:"2017-04-03T18:30:00.000Z"

after it leaves my code, and I see the same in network tab, it is converted to UTC date: "2017-04-03T18:30:00.000Z"

我搜索了根据需要在我的index.html中包括angular的语言环境文件的解决方案:

I searched for the solution according to which I need to include locale file of angular in my index.html which I did:

<script type="text/javascript" src="resources/js/angular/angular-locale_en-in.js"></script>

但是没有帮助.我已经看到了将日期格式添加到过滤器之类的解决方案,但是我想要一个全局解决方案.有什么帮助吗?谢谢:)

but it doesn't help. I've seen solutions like adding date format to filter or something, but I want a global solution. Any help? Thanks :)

推荐答案

处理日期,时间和时区也使我感到困惑.可能是这个答案为您提供了一些有关如何处理它们的见识.

Handling date, time, and timezone have confused me too. May be this answer gives you some insight on how you can handle them.

在Chrome开发者控制台中尝试以下代码,并查看如何以不同的格式显示同一日期:

Try the following code in Chrome's developer console and see how same date is presented in different formats:

var date = new Date();
date.toISOString(); // "2017-04-29T09:54:28.714Z"
date.toGMTString(); //"Sat, 29 Apr 2017 09:54:28 GMT"
date.toLocalString(); //"4/29/2017, 3:24:28 PM"

您在客户端上创建的任何日期始终将日期记录为零时区偏移,即 UTC +/- 00:00 Z .为简单起见,您可能认为UTC和GMT相同.当出于显示目的时,会根据浏览器的时区显示相同的日期.如果您执行 console.log(日期),则会输出 Sat Apr 29 2017 15:24:28 GMT + 0530(IST),但这并不意味着日期的内部记录是根据浏览器的时区进行的.它只是根据浏览器的时区显示在屏幕/控制台上.

Any date that you create on client always records the date at zero timezone offset i.e. UTC+/-00:00 Z. For simplicity you may think UTC and GMT as same. When it comes to display purpose the same date is presented as per the browser's timezone. If you do console.log (date) it'll output Sat Apr 29 2017 15:24:28 GMT+0530 (IST) but that doesn't mean that the internal recording of the date is as per browser's timezone. It's just presented on screen/console as per browser's timezone.

将日期表示形式视为不是从一个时区转换为另一个时区,而是将它们视为同一日期的不同表示形式.在您的浏览器中,它表示为 GMT + 0530 偏移量,当它发送到服务器时,它是在零时区偏移量的同一日期.

Look at date representations not as being converted from one timezone to another but look at them as different representation of the same date. In your browser it is represented as GMT+0530 offset and when it is sent to server it is the same date at zero timezone offset.

根据您的评论,如果您选择格林威治标准时间+0530时区的4月4日上午00:00,内部将是格林尼治标准时间+0的4月3日下午18:30,即零时区偏移.让它按原样转到服务器.当您需要使用该日期时,它会从4月3日起从服务器返回,并会根据浏览器的时区偏移量显示在浏览器中.不涉及转换,它是一个具有不同表示形式的日期.

As per your comment, if you choose 4th Apr at 00:00 AM in GMT+0530 timezone, internally it'll be 3rd Apr at 18:30 PM in at GMT+0 i.e. zero timezone offset. Let it go to server as it is. When you need to use this date, it comes back from server as 3rd Apr and it'll be displayed in browser as per the browser's timezone offset. There is no conversion involved, it is one date with different representation.

我曾经问过一个相关问题,也许这可以增加更多的说明.

I once asked a related question, may be this adds more clarification.

总的来说,这个答案仍然与@geminiousgoel和@charlietfl答案相同.

And overall, this answer is still same as @geminiousgoel and @charlietfl answers.

这篇关于角度$ http.post将日期更改为UTC日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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