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

后端 未结 6 1772
遇见更好的自我
遇见更好的自我 2021-02-05 00:44

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 ?         


        
相关标签:
6条回答
  • 2021-02-05 01:19

    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.

    0 讨论(0)
  • 2021-02-05 01:28
    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, ...

    0 讨论(0)
  • 2021-02-05 01:29
    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
        }
    
    0 讨论(0)
  • 2021-02-05 01:31
    var end = DateTime.Parse(oldDate);   
     if (start.Hour == end.Hour && start.AddMinutes(20).Minute >= end.Minute)
    
    0 讨论(0)
  • 2021-02-05 01:35
    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  
    }
    
    0 讨论(0)
  • 2021-02-05 01:35

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

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

    Inspect TotalMinutes.

    0 讨论(0)
提交回复
热议问题