54

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 :)

The Mask
  • 17,007
  • 37
  • 111
  • 185
  • You might want to use `DateTime.UtcNow` instead of `DateTime.Now` to avoid issues with daylight savings time changes when the clock can leap back or forward. – Ian Mercer Oct 09 '11 at 03:20
  • 2
    @Hightechrider: Using `UtcNow` is only useful if the comparison timestamp is in UTC! – Gabe Oct 09 '11 at 03:30
  • 1
    @Gabe, obviously yes, but converting the start time to UTC and comparing it is going to be better than comparing to `DateTime.Now` because otherwise you can get situations where an hour appears to have elapsed when it hasn't! – Ian Mercer Oct 09 '11 at 03:38

6 Answers6

111
  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:

    DateTime.UtcNow > start.AddMinutes(20)

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, ...

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
27
var start = DateTime.Now;
var oldDate = DateTime.Parse("08/10/2011 23:50:31");

if ((start - oldDate).TotalMinutes >= 20)
{
    //20 minutes were passed from start  
}
VoidStar
  • 5,241
  • 1
  • 31
  • 45
  • This doesn't work if the hour is passed. for start = `23:20` end = `24:20` it's true. – The Mask Oct 11 '11 at 04:22
  • ¿Why would you send 24:20 as the time? In such case, you should use a TimeSpan object, not a DateTime, DateTime objects are meant to be used for valid time representations, not "24:20" – Marcel Valdez Orozco Oct 23 '11 at 19:29
7
var start = DateTime.Now;
var oldDate = DateTime.Parse("08/10/2011 23:50:31"); 

    if(start.Subtract(oldDate) >= TimeSpan.FromMinutes(20)) 
    {
     //20 minutes were passed from start
    }
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
6

Parse oldDate into a DateTime object (DateTime.Parse).

Subtract the parsed date from start. This will return a TimeSpan.

Inspect TotalMinutes.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • 2
    +1 this answer is better than mine. I forgot about TotalMinutes...this answer prevents an unnecessary second TimeSpan. – Steve Danner Oct 09 '11 at 03:00
  • 2
    Personally I prefer `start.AddMinutes(20) > now` because it says "more than 20 minutes have elapsed since the start". Also, you can use it to test for intervals measured in weeks, months, years which you cannot easily do with a TimeSpan. – Ian Mercer Oct 09 '11 at 03:25
1

I was able to accomplish this by using a JodaTime Library in my project. I came out with this code.

String datetime1 = "2012/08/24 05:22:34";
String datetime2 = "2012/08/24 05:23:28";

DateTimeFormatter format = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");
DateTime time1 = format.parseDateTime(datetime1);
DateTime time2 = format.parseDateTime(datetime2);
Minutes Interval = Minutes.minutesBetween(time1, time2);
Minutes minInterval = Minutes.minutes(20);

if(Interval.isGreaterThan(minInterval)){
  return true;
}
else{
  return false;
}

This will check if the Time Interval between datetime1 and datetime2 is GreaterThan 20 Minutes. Change the property to Seconds. It will be easier for you know. This will return false.

John Paul Manoza
  • 1,735
  • 25
  • 22
  • I know i am very late (juste incase someone sees it nowadays) but you can now use TimeSpan to accomplish that like : TimeSpan ts = date2 - date1; and then have the result in ts.TotalMinutes – Max Apr 01 '20 at 15:49
0
var end = DateTime.Parse(oldDate);   
 if (start.Hour == end.Hour && start.AddMinutes(20).Minute >= end.Minute)
Kakashi
  • 2,165
  • 14
  • 19