1

When passing a date in javascript, that has a 00:00 time, to a webservice that is using linq to sql to access the DB, on some clients browsers, after the save, the date that was passed to the datetime column in the db now has a time that is no longer 00:00, some cases it is hours before midnight so the date is moved into the previous days date, other times it is hours before.

How do we always make the date save and not the time to the db?

This is using asp.net webforms and c# and asmx webservices and most queries are compiled.

SetiSeeker
  • 6,482
  • 9
  • 42
  • 64

2 Answers2

0

It depends on the details of how the date is being encoded. At a high level though, you have to:

  1. Make sure the timezone doesn't get wiped out when it's sent from client to server. That means, send it as either a date-only string, or as a date with the timezone preserved; not as a UTC date.
  2. Make use of DateTimeKind (UTC or local) and/or DateTimeOffset on the server to ensure it is properly represented whenever you're sending/receiving to the client/database.

See also here: Linq to SQL DateTime values are local (Kind=Unspecified) - How do I make it UTC?

Community
  • 1
  • 1
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
0

I'm guessing that the date and time on the client is important. In that case how about converting the date-time info to iso format before sending it to the server and also send the client's timezone offset as well.

var d = new Date
d.toISOString() // 2012-05-05T22:14:35.506Z
// or maybe jus
d.getTime() // milliseconds since jan 1st 1970 or thereabouts
d.getTimezoneOffset() // Timezone offset in minutes from UTC

This way you get the UTC date and time and the timezone offset as well, that is how many minutes UCT time differs from local time. For example Norway would have a negative offset (UTC-Norwegian time = negative).

Helgi
  • 1,577
  • 9
  • 9