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

后端 未结 6 1832
遇见更好的自我
遇见更好的自我 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: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, ...

提交回复
热议问题