1

I have to situations where I transfer dates from browser to server.

1) First situation is through a jQuery/JSON call to an ASMX service (.NET 4.0).

2) The second situation is when page first serializes an object, which includes a date, to a hidden field and then the page is posted to server. The hidden field is parsed (using .NET's built in JavaScriptSerializer).

So, let's say the user enters the date "2011-08-31". In the first situation the resulting date on the server is 2011-08-30 22:00 (because I am currently in a +2 time zone). However, in the second situation the resulting date on the server is 2011-08-31 (correct).

Both situtations use the same JSON serializer on the browser.

But I cannot understand why these two are generating different dates on server :-( Does anyone know?

Sten
  • 1,079
  • 11
  • 25

1 Answers1

1

You need to use UTC dates, take a look at these resources:

get UTC Date in javascript

converting UTC with C#

Community
  • 1
  • 1
Dmitry Samuylov
  • 1,554
  • 2
  • 14
  • 37
  • Thanks! Problem is that since I am using ASMX, I would like this conversion to be made automatically - otherwise I need to convert each date serverside. Also: I have tried to alter the JSON stringifyer so that I really get "2011-08-31T00:00:00Z" (which can be interpreted properly by .NET serverside). But I still have the same difference between the two scenarios above. – Sten Aug 31 '11 at 07:48
  • What are you planning to do with this date on the server? You're not persisting it to some kind of datastorage? Did you try using the JavaScript function `Date.getUTCDate()` before you pass the date to your web-service call? – Dmitry Samuylov Aug 31 '11 at 07:57
  • The date will be stored in an SQL Server database. Column data type is old time DateTime (which excludes any time zone info, afaik). I have been trying various methods for serializing/stringifying the date. Currently, I stringify them using ISO8601 format ("2009-02-15T00:00:00Z") - and assuming the user enters YYYY-MM-DD. – Sten Aug 31 '11 at 08:22