Reputation: 850
I have an web api that return events. Each event occurs at a given date which is abstracted as a DateTimeOffset. Now on the server I want to check if a given event has occured according to the event datetimeoffset, not the server datetimeoffset.
For example an event has datetimeoffset 2014-02-06 10:00:00+0300, server is datetimeoffset 2014-02-06 10:00:00+0100. How can I check if the event has already happened?
Upvotes: 1
Views: 167
Reputation: 4873
I believe you could just compare the two DateTimeOffset
directly. Or, you could use the UtcDateTime
property to get the UTC value of the DateTimeOffset
and use that for comparison.
Example:
var eventTime = new DateTimeOffset(2014, 2, 6, 10, 0, 0, 0, TimeSpan.FromHours(3));
var serverTime = new DateTimeOffset(2014, 2, 6, 10, 0, 0, 0, TimeSpan.FromHours(1));
var otherTime = new DateTimeOffset(2014, 2, 6, 10, 0, 0, 0, TimeSpan.FromHours(-1));
Console.WriteLine("\tLocalTime\t\t\tUtcTime\t\tInThePast");
Console.WriteLine("Server\t{0}\t{1}",serverTime, serverTime.UtcDateTime);
Console.WriteLine("Event\t{0}\t{1}\t{2}", eventTime, eventTime.UtcDateTime, eventTime < serverTime);
Console.WriteLine("Other\t{0}\t{1}\t{2}", otherTime, otherTime.UtcDateTime, otherTime < serverTime);
Would produce:
LocalTime UtcTime InThePast
Server 2/6/2014 10:00:00 AM +01:00 2/6/2014 9:00:00 AM
Event 2/6/2014 10:00:00 AM +03:00 2/6/2014 7:00:00 AM True
Other 2/6/2014 10:00:00 AM -01:00 2/6/2014 11:00:00 AM False
Upvotes: 1
Reputation: 9537
You could use the Offset Property which returns a TimeSpan object within a range from -14 hours to 14 hours.
Having that property, you could easily compute if an event is already happened.
Upvotes: 0