Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date comparison - How to check if 20 minutes have passed?

Tags:

c#

.net

datetime

How to check if 20 minutes have passed from current date?

For example:

var start = DateTime.Now; var oldDate = "08/10/2011 23:50:31";       if(start ??) {      //20 minutes were passed from start     } 

what's the best way to do this? Thanks :)

like image 877
The Mask Avatar asked Oct 09 '11 02:10

The Mask


People also ask

How do I subtract hours and minutes in C#?

Add(new TimeSpan(-2,0,0)); DateTime original = new DateTime(year, month, day, 17, 30, 0); DateTime updated = original. Add(new TimeSpan(0,-45,0)); Or you can also use the DateTime. Subtract(TimeSpan) method analogously.

How can check date less than current date in C#?

ParseExact(input, "MM/dd/yyyy", CultureInfo. InvariantCulture); int result = DateTime. Compare(dTCurrent, inputDate); The int 'result' would indicate if dTCurrent is less than inputDate (less than 0), same as (0) or greater than (greater than 0).


1 Answers

  1. You should convert your start time to a UTC time, say 'start'.
  2. You can now compare your start time to the current UTC time using:

    start.AddMinutes(20) > DateTime.UtcNow

This approach means that you will get the correct answer around daylight savings time changes.

By adding time to the start time instead of subtracting and comparing the total time on a TimeSpan you have a more readable syntax AND you can handle more date difference cases, e.g. 1 month from the start, 2 weeks from the start, ...

like image 169
Ian Mercer Avatar answered Sep 20 '22 08:09

Ian Mercer