where clause with timezones
I'm writing a where clause for a linq-to-sql query. Let's say a column name is AppointTime which is stored in GMT timezone. The query I'm writing is dynamic and the UI allows the 开发者_如何学运维user to select the appointments set today, yesterday, 2 days ago, 3, 5, 10, 30, 60, 90 days ago.
I have the user's timezone in a decimal (ie -9.0 for US PST time).
What's the best way to write the where clause so that the user who wants to see the appointments he set 2 days ago comes up in the query's result set.
Something like this?
int days = 2; // set by UI?
var upcoming =
from appointment in dc.Appointments
where appointment.AppointTime.AddHours(userTZ) < DateTime.UtcNow.AddDays(days)
&& appointment.AppointTime > DateTime.UtcNow
orderby appointment.AppointTime ascending
select appointment;
I confess I'm not totally clear on exactly what info you're trying to get... but this should hopefully contain enough clues to write the query you need. Remember two things - Addx()
functions on DateTime
can take a negative value to subtract; and times in the future are greater than times in the past, when comparing two DateTimes
.
精彩评论