7

I've noticed, that in case my C# query contains

(e => e.MyDateTimeData == DateTime.Now)

the generated SQL uses

WHERE [e].[MyDateTimeData] = GETDATE()

When the EF client timezone is different than the SQL Server host SQL Server process user timezone, this will not be good.

The timezones could be different even the client process and the SQL Server process run on the same host.

Missing I something, or is this a real issue?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • 1
    GETDATE() value is derived from the operating system of the computer on which the instance of SQL Server is running (not client computer settings) – Vitaly Borisov May 20 '19 at 20:13
  • you should decide which date what you want to use. Database GETDATE or application or client side and do not combine with each other. I am using application date and store the date from object into db. My db do nothing with dates. – daremachine May 20 '19 at 20:15
  • @VitalyBorisov Indeed, that is what my question about.... – g.pickardou May 21 '19 at 03:51
  • @daremachine You are right. When developer uses DateTime.Now he decided to use the client time with client timezone, but it seems EF changes this to server time with server timezone – g.pickardou May 21 '19 at 03:58
  • [Don't use `DateTime`, use `DateTimeOffset`](https://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset) – Erik Philips May 07 '21 at 19:42
  • @ErikPhilips, have you tried the same issue with `DateTimeOffset.Now`? My bet is the issue is the same, I mean: I instruct the code to use client's clock, and it is uses server's. This is not a *timezone* matter, this is about fidelity and consistence. See @David Browne's answer, the two code, his and mine should do the very same. Besides of all above, DateTimeOffset is preferred, but this nothing to do with the issue. – g.pickardou May 11 '21 at 08:33
  • My comment was not in regards to `.Now`. My comment was not on topic nor are comments required to be on topic. – Erik Philips May 12 '21 at 03:53

1 Answers1

3

If you want to use the client time the then pass it in as a parameter, like this:

var dt = DateTime.Now;
. . .
var q = db.SomeTable.Where(e => e.MyDateTimeData == dt)
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • The result of your code and my code should not differ, still differs. So this is an Issue, if I understand correctly – g.pickardou May 21 '19 at 03:49