JSON.stringify更改日期 [英] JSON.stringify changes date

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

问题描述



现在,
在网站的时候,我有一个网站,它有功能登录特定的时区,而不管客户端的时区是什么。用户在对话框中选择一个日期。我正在使用JSON.stringify与服务器端发送几个其他属性。



但是,只要在服务器端接收到日期更改。



示例: -



我使用(+05:30)登录印度时区01/08/2015 00:00:00,服务器正在使用卡萨布兰卡时区。



当服务器端收到日期时,日期减少一个31/08/2015。



我认为是因为时区转换。



我已经检查过以下链接: -



JSON Stringify更改因为UTC的日期时间



我已经尝试了这个答案: - http://stackoverflow.com/a/1486612/2592727



但是我无法理解该公式如何工作。所以更好的是获得更多的细节,并使用一些具体的解决方案。



要求: -



我允许用户只选择日期。
我想要通过服务器端接收相同的日期。



我该如何完成这个?请尽可能详细描述更多细节。



有没有什么简单的方法来避免这种冲突?

解决方案

JSON.stringify 方法将日期作为UTC存储在字符串中,当您在.NET中解析时,您将获得 DateTime 包含完全相同的时间点,但转换为服务器的本地时间。



您可以处理这个两种方式,您可以将时间转换回原始时区,也可以避免使用 Date 数据类型。



当然,转换到原始时区需要知道时区是什么。例如:

  var zone = TimeZoneInfo.FindSystemTimeZoneById(India Standard Time); 
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(date.ToUniversalTime(),zone);

为避免使用 Date 类型,您将序列化之前将日期格式化成字符串。示例:

  var s = date.getFullYear()+' - '+(date.getMonth()+ 1)+' - '+ date.getDate(); 

在服务器端,您将使用相同的格式解析字符串:

  DateTime userTime = DateTime.ParseExact(date,yyyy-Md,CultureInfo.InvariantCulture); 


I am having one website in which it has functionality to Login with specific timezone regardless what's the timezone on client side.

Now, In website when user selects a date in dialog.I am sending it to server side using JSON.stringify with several other properties.

But whenever it is received at server side date is changed.

Example :-

I logged in using (+05 : 30) India time zone "01/08/2015 00:00:00" and server is having Casablanca Timezone.

When the date is received at server side the date is reduced by One "31/08/2015".

I think it is because of timezone conversion.

I already checked following link :-

JSON Stringify changes time of date because of UTC

I already tried that answer :- http://stackoverflow.com/a/1486612/2592727

But i am unable to understand how that formula works. So it's better to get more details and work with some specific solution.

Requirement :-

I am allowing user to select only date. I want same date to be received over server side.

How can i accomplish this? Please describe with more details if possible.

Is there any simple way to avoid this collision?

解决方案

The JSON.stringify method stored the date as UTC in the string, and when you parse that in .NET you will get a DateTime that contains the exact same point in time, but converted to the local time of the server.

You can handle this in two ways, you can either convert the time back to the original time zone, or you can avoid using the Date data type.

Converting to the original time zone of course requires you to know what the time zone is. Example:

var zone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(date.ToUniversalTime(), zone);

To avoid using the Date type, you would format the date into a string before serialising it. Example:

var s = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

On the server side you would parse the string using the same format:

DateTime userTime = DateTime.ParseExact(date, "yyyy-M-d", CultureInfo.InvariantCulture);

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

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