Prevent JsonResult from automatically formatting Dates

I am writing a cloud-based program that uses UTC to save the date on the server and converts it back to a round trip. The problem is that my instance in the cloud will automatically convert the JsonResult date and time values โ€‹โ€‹according to the localization parameters in the browser, getting the result.

I went through many steps to make sure that the server-side code at both levels returns the correct data, and that it is in all instances, and not a single Javascript code on my page makes changes (even deleted all my Date formatting in Javascript just in case ), I followed with Firebug to determine the exact point at which it changes when my page receives Json Result from my JsonResult method. As I said, I debugged the values โ€‹โ€‹before sending to the page, and they are correct, and in my opinion they change depending on the location of the browser ....

Has anyone had a similar problem?

Return value: Date (1341792000000) 08/08/2012 17:00

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

thanks

+4
source share
3 answers

Finally, I got the right results, with many changes in my application. I did a lot of things to make this happen ... First, I used timezone.JS to get the list of time zones that will be used in the application, and use jstz to get the current time zone of the browser loading the page. Next, I have to do (for mvc) a method for obtaining a file that accesses time zones for loading in timezoneJS.

Then, while saving the time zone, I specified pst as a type, and then convert back to utc in roundtrip to update the interface.

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

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 you to run cloud projects on any server and display in any browser, regardless of the time zone, and also allows the user to view and configure data sensitive to the time zone from the beginning and allow users to see the start and end dates of the custom database values.

+1
source

Have you tried

 date.toLocaleString() 

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

0
source

All Articles