Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey, i'm trying to compare 2 dates to see if it has been 3 days. I'm using UTC time for this. For some reason it is not working and I am continuing to scratch my head why.

What I have tried:

DateTime time = DateTime.Parse("6/20/2018 4:38:26 AM");


if (DateTime.UtcNow > time.AddDays(3))


If someone could help me out with that if statement, that would be great!
Posted
Updated 13-Jun-18 19:22pm
v2

Maybe try this:
if (DateTime.Compare(DateTime.UtcNow, time.AddDays(3)) > 0)
 
Share this answer
 
Comments
[no name] 14-Jun-18 1:22am    
Alright, thank you. Turns out I was being a complete fool as the other code was working too. I kept moving the date forward instead of backwards...
The comparison looks fine and behaves as expected on my machine.

The interpretation of the date could be the problem. It parses the date based on the date settings of your computer. On my computer (Dutch, nl-NL) it throws an exception unless I either:

- Change the format:
DateTime time = DateTime.Parse("20-06-2018 04:38:26");


- Set the thread culture to en-US:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900