JSON Stringify由于UTC而更改日期时间 [英] JSON Stringify changes time of date because of UTC

查看:259
本文介绍了JSON Stringify由于UTC而更改日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JavaScript日期对象总是以UTC +2表示,因为我所在的位置。因此像这样

  Mon Sep 28 10:00:00 UTC + 0200 2009 

问题是在执行 JSON.stringify 将上述日期转换为

  2009-09-28T08:00:00Z(注意2小时缺失,即8而不是10)
pre>

我需要的是日期和时间得到尊重,但不符合要求,因此应该是

  2009-09-28T10:00:00Z(这是怎么回事)

基本上我使用这个:

  var jsonData = JSON.stringify(jsonObject);我尝试传递一个替换参数(第二个参数在stringify上),但问题是该值已经有已被处理。



我还尝试使用 toString() toUTCString()在日期对象,但这些不给我我想要的任何一个。



任何人可以帮助我吗?

解决方案

最近我遇到了同样的问题。并使用以下代码解决:

  x = new Date(); 
x.setHours(x.getHours() - x.getTimezoneOffset()/ 60);


My date objects in JavaScript are always represented by UTC +2 because of where I am located. Hence like this

Mon Sep 28 10:00:00 UTC+0200 2009

Problem is doing a JSON.stringify converts the above date to

2009-09-28T08:00:00Z  (notice 2 hours missing i.e. 8 instead of 10)

What I need is for the date and time to be honoured but it's not, hence it should be

2009-09-28T10:00:00Z  (this is how it should be)

Basically I use this:

var jsonData = JSON.stringify(jsonObject);

I tried passing a replacer parameter (second parameter on stringify) but the problem is that the value has already been processed.

I also tried using toString() and toUTCString() on the date object, but these don't give me what I want either..

Can anyone help me?

解决方案

Recently I have run into the same issue. And it was resolved using the following code:

x = new Date();
x.setHours(x.getHours() - x.getTimezoneOffset() / 60);

这篇关于JSON Stringify由于UTC而更改日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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