How to check if a datetime is equal to 20:00 *local* time
I have a requirement in my application where we have to perform certain activities based on the time (that too local to the client)... For example - if the client is in US and the local time at the client is 20:00 then do something...
So far to simplify we are taking just one timezone for each country.. Any pointers how can I check if the time开发者_开发百科 is 20:00 at the client based on the timezone??
Assuming you're using .NET 3.5 or higher, you'll want the TimeZoneInfo
for the client. Then you can use:
DateTimeOffset clientNow = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow,
timeZone);
if (clientNow.TimeOfDay == TimeSpan.FromHours(20))
{
// ...
}
(Of course, that would only work at the exact instant of 20:00. You'll want to adjust it according to your real requirements.)
Use quartz.net to schedule a job instead. It is very easy to work with an it is very stable as well.
If you need the timezone thing, just trigger the job every hour and fire the activity you want for the clients that apply.
boolean isDateTimeEqualToTwentyAught() {
return false;
}
Keep in mind that there's a bug, err, feature in DateTime class that allows you to compare values in different timezones completely ignoring said timezones. Test, test, and retest to make sure it's what you want.
精彩评论