0

I hate dates, I can never get them to behave. I have a javascript variable that looks like this:

var currentDate = new Date();

I pass this to a C# Web API controller as a parameter. My local time was 12:43 but when I put a breakpoint in my action it shows 11:43. The problem is, that if I do this at 00:43 then my controller would take the date as yesterday. I need it to pick out the right day. If I select the currentDate as 02/09/2015 12:43 then I need my controller to use the same date.

I know this has something to do with local times etc, but how can I get them all to use the same one?

Any help would be greatly appreciated.

r3plica
  • 13,017
  • 23
  • 128
  • 290
  • You may need to do all your work with UTC dates. I'm not sure about javascript, but .NET has that capability. – rory.ap Sep 02 '15 at 11:50
  • Yeah, I already use UTC dates with C# but the problem is what JavaScript is passing. – r3plica Sep 02 '15 at 11:51
  • passing dates between C# and javascript is always going to be hit and miss - we've resorted to using string representations (yyyymmdd) - it's messy, but guaranteed accurate – Jaromanda X Sep 02 '15 at 11:54
  • 1
    Send it to your API as a string. Then convert the string to a Date inside your controller. Of course, continue to use UTC time. – krillgar Sep 02 '15 at 11:54
  • there's a lot of issues when dealing with js dates. i suggest using a package called "moment.js" and moment.timezone.js to help with the dates and all – Daemedeor Sep 02 '15 at 11:55
  • 1
    I suffered same issue. Now we used string parameters as its said here. http://stackoverflow.com/questions/6702705/how-to-convert-javascript-datetime-to-c-sharp-datetime – Morcilla de Arroz Sep 02 '15 at 11:56
  • try converting your date to utc in javascript before sending it to server. – Ankit Sahrawat Sep 02 '15 at 11:57
  • Yup using strings is what seems to work best. Let c# work it all out – r3plica Sep 08 '15 at 10:04

0 Answers0