防止JsonResult自动格式化日期

时间:2021-09-10 07:23:42

I'm writing a cloud based program that utilizes UTC for date saving to the server, and convert it back on the round trip. Problem is, my instance in the cloud is autoconverting the JsonResult datetime values according to the localization settings on the browser obtaining the result.

我正在编写一个基于云的程序,它利用UTC将日期保存到服务器,并在往返时将其转换回来。问题是,我在云中的实例是根据获取结果的浏览器上的本地化设置自动转换JsonResult日期时间值。

I have gone through tons of steps to make sure the server side code on both levels is returning the proper data, and it is on all instances, and no Javascript code on my page is making the change either (even removed all my Javascript date formatting just in case), I have traced out with Firebug to determine the exact point that it changes, is when my page receives the Json Result from my JsonResult method. Like I said, I have debugged the values before being sent to the page, and they are proper, and on my response, they are modified based upon browser location....

我已经完成了大量的步骤,以确保两个级别上的服务器端代码都返回正确的数据,并且它在所有实例上,并且我的页面上的任何Javascript代码都没有进行更改(甚至删除了我的所有Javascript日期格式为了以防万一),我已经用Firebug进行了追踪,以确定它改变的确切点,就是当我的页面从我的JsonResult方法接收到Json结果时。就像我说的那样,我在发送到页面之前调试了它们,并且它们是正确的,并且在我的响应中,它们是根据浏览器位置进行修改的....

Has anyone had a similar problem?

有没有人有类似的问题?

Value returned:Date(1341792000000) 07/08/2012 17:00

返回值:日期(1341792000000)07/08/2012 17:00

Should be: Date(1341817200000) (07/09/2012 12:00AM)

应该是:日期(1341817200000)(07/09/2012 12:00 AM)

Thanks

谢谢

3 个解决方案

#1


1  

I finally ended up getting the proper results, with many modifications to my application. I did a lot of stuff to make this happen... First, I implemented timezone.JS to get a listing of timezones that will be used within the application, and used jstz to get current timezone of browser loading the page. Next, I have to make (for mvc) an file get method that accesses the timezones to load into timezoneJS.

我最终得到了正确的结果,对我的应用程序进行了很多修改。我做了很多事情来实现这一点......首先,我实现了timezone.JS来获取将在应用程序中使用的时区列表,并使用jstz来获取浏览器加载页面的当前时区。接下来,我必须(为mvc)创建一个文件get方法,该方法访问时区以加载到timezoneJS中。

Next, on save of the timezone, I specified pst as the type, and then convert back to utc on roundtrip to update the interface.

接下来,在保存时区时,我将pst指定为类型,然后在roundtrip上转换回utc以更新接口。

On formatting of my Json date, I run the timezoneJS method and get the timezone name from jstz, and set the new date value like such:

在格式化我的Json日期时,我运行timezoneJS方法并从jstz获取时区名称,并设置新的日期值,如下所示:

var timezone = jstz.determine();
timezoneJS.timezone.zoneFileBasePath = '/Item/GetTz'; // get file method
var dt = new timezoneJS.Date(parseInt(jsonDate.substr(6), timezone.name())); // strips out date from json date
dt.setTimezone('America/Los_Angeles');

This allows on the cloud projects to be ran on any server, and displayed in any browser regardless of timezone, and allow the user to view and configure timezone sensitive data natively, and allow for users to see the start/end date of configurable database values.

这允许云项目在任何服务器上运行,并在任何浏览器中显示,无论时区如何,并允许用户本地查看和配置时区敏感数据,并允许用户查看可配置数据库值的开始/结束日期。

#2


0  

Maybe using http://msdn.microsoft.com/en-us/library/system.datetime.specifykind.aspx

也许使用http://msdn.microsoft.com/en-us/library/system.datetime.specifykind.aspx

#3


0  

Did you try with,

你试过吗,

date.toLocaleString()

Alternatively, You can create a new Date object and use Date.setUTC

或者,您可以创建一个新的Date对象并使用Date.setUTC

#1


1  

I finally ended up getting the proper results, with many modifications to my application. I did a lot of stuff to make this happen... First, I implemented timezone.JS to get a listing of timezones that will be used within the application, and used jstz to get current timezone of browser loading the page. Next, I have to make (for mvc) an file get method that accesses the timezones to load into timezoneJS.

我最终得到了正确的结果,对我的应用程序进行了很多修改。我做了很多事情来实现这一点......首先,我实现了timezone.JS来获取将在应用程序中使用的时区列表,并使用jstz来获取浏览器加载页面的当前时区。接下来,我必须(为mvc)创建一个文件get方法,该方法访问时区以加载到timezoneJS中。

Next, on save of the timezone, I specified pst as the type, and then convert back to utc on roundtrip to update the interface.

接下来,在保存时区时,我将pst指定为类型,然后在roundtrip上转换回utc以更新接口。

On formatting of my Json date, I run the timezoneJS method and get the timezone name from jstz, and set the new date value like such:

在格式化我的Json日期时,我运行timezoneJS方法并从jstz获取时区名称,并设置新的日期值,如下所示:

var timezone = jstz.determine();
timezoneJS.timezone.zoneFileBasePath = '/Item/GetTz'; // get file method
var dt = new timezoneJS.Date(parseInt(jsonDate.substr(6), timezone.name())); // strips out date from json date
dt.setTimezone('America/Los_Angeles');

This allows on the cloud projects to be ran on any server, and displayed in any browser regardless of timezone, and allow the user to view and configure timezone sensitive data natively, and allow for users to see the start/end date of configurable database values.

这允许云项目在任何服务器上运行,并在任何浏览器中显示,无论时区如何,并允许用户本地查看和配置时区敏感数据,并允许用户查看可配置数据库值的开始/结束日期。

#2


0  

Maybe using http://msdn.microsoft.com/en-us/library/system.datetime.specifykind.aspx

也许使用http://msdn.microsoft.com/en-us/library/system.datetime.specifykind.aspx

#3


0  

Did you try with,

你试过吗,

date.toLocaleString()

Alternatively, You can create a new Date object and use Date.setUTC

或者,您可以创建一个新的Date对象并使用Date.setUTC