1

I have an MVC5 Web Application as web interface for my time recording app. My android app sends its times as string to server, there I parse them to .NET DateTime and store it in Database. When I load the time records again and create a JavaScript array I get one additional hour added. I already tried a lot but this post seems to solve the problem: stackoverflow

new Date(date.valueOf() + date.getTimezoneOffset() * 60000);

My question is: Is this the correct way to do? I don’t need any culture or time zones for time recording. A time record that starts at 6 am and ends at 6pm has always this values. Actually it even MUST have this values! Isn’t there a way to tell the JavaScript Date that it should do nothing with my time? Just hold the value I set to it?

UPDATE/Extension: My web inteterface is a MVC5 application and the communication with android phones is done with a WCF service. To make the the code I posted above run I kicked out the JavaScriptSerializer and wrote the JSON array with StringBuilder. This gave me the possibility to include the snipped posted above and let it be executed on the client. I also tried all DateTime.Kind's (local, unspecified and UTC) in combination with the JavascriptSerializer but with no success.

Again, I need unmodified datetimes on client side.

With the answer of Matt I now see three options:

  1. My code posted above
  2. Parsing the DateTime and append a Z for GMT
  3. Add SerializerSettings.DateTimeZoneHandling in Global.ascx Application_Start.

Cheers, Stefan

Community
  • 1
  • 1
stefan
  • 1,336
  • 3
  • 21
  • 46

1 Answers1

1

It's difficult to tell from the question, but you need to be working with Coordinated Universal Time (UTC) when storing your dates - it just makes things easier to work with later on. Javascript dates all work in local time, but if the server is sending down UTC time, you can easily convert.

So if the server send down (e.g. in JSON), your date value like "2014-03-09T21:25:05.937Z", then you can get it in the user's local time using:

var localDate = new Date(Date.parse("2014-03-09T21:25:05.937Z"))

This gives localDate a value of Mon Mar 10 2014 10:25:05 GMT+1300 (New Zealand Daylight Time) (in my time zone).

The crucial bit is the 'Z' at the end of the date string. This marks it as Zulu-time/UTC ... from there, javascript can convert for you just fine.

If you are using a WebAPI, then the following should help get things going, even with stored local dates:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
Matt Tester
  • 4,663
  • 4
  • 29
  • 32
  • I work with “DateTimeKind.Unspecified“ but I also tried the other two options. Then I kicked out the .NET JavascriptSerializer and wrote my JSON string manually so that I can add the JS-code posted above. I don’t use WebAPI but I can register “json.SerializerSettings.DateTimeZoneHandling “ in Global.ascx in Startup, right? I have a WCF service for android and a MVC5 app for the webinterface. So we have three options. Which one would you recommend? – stefan Mar 17 '14 at 23:49
  • 1
    `DateTimeKind.Unspecified` just means that the `DataTime` object is making no assumptions at to whether it is local or UTC time. So, just ensure you know whether it is really UTC or not - the date received by the WCF service ... is it UTC? If not, convert it. Then you'll be storing UTC dates and be on the right path as in the answer. – Matt Tester Mar 17 '14 at 23:54
  • The WCF side is working fine. As mentioned I transfer my DateTimes as string from and to Android and therefore DateTimeKind.Unspecified seems to be correct. To be more specific: When android sends data I parse the datetime-string and use this constructor: DateTime(Int32, Int32, Int32, Int32, Int32, Int32) which implicitly sets “DateTimeKind.Unspecified”. When I send/receive DateTimes to/from android I use this custom format “5.2.2013/19:26”. However, the WCF is not involved here. I have a website which allows the user to login on pc and edit the data which was synced from android before. – stefan Mar 18 '14 at 00:12
  • For me the problem seems to be clear. I store my DateTime’s with implicitly set DateTimeKind.Unspecified in database. Implicitly because I use this constructor “DateTime(I,I,I I,I,I)“ to create DateTime from Year,Month,Day,Hour,Minute and Second fields. Now I need to create a JSON array for my JQuery plugin which needs Start and End as JavaScript-Date and here the problems begin. Since JavaScript date want to be local the data I display is not correct. Sample: The user created a time record from 6am to 7am and on the website the same entry would be 6am + UTC offset to 7am + UTC offset – stefan Mar 18 '14 at 00:29