I want to write if If statement that executes if the CreatedDate is on or before the next 1 hour. Is there a way to do this?
Something like:
if (CreatedOn.ToUniversalTime() <= DateTime.Now.AddHours(1).ToUniversalTime())
{
}
Would that be right or is there a better way?
Thanks!
I think your approach is mostly fine. After all, look at your description:
That doesn’t talk about subtracting one time from another – it talks about comparing
CreatedDatewith “the next hour” i.e. one hour from now.So:
that looks pretty clean to me – except you need to be aware of what
CreatedOnreally is. Is it local? Unspecified? Already universal? UnfortunatelyDateTimeis problematic in this respect… if you were using Noda Time there’d be no cause for doubt 😉