Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the difference between 2 times, and then compare the difference to see if it is less than 5 minutes

Tags:

c#

.net

I want to calculate the difference between two times and then compare difference is less than 5 MIN.. Please note I want difference in min. using c#.net

like image 804
hrishi Avatar asked Jan 20 '10 14:01

hrishi


People also ask

How do you calculate the difference in minutes between two times?

To get the time difference in a single time unit (hours ,minutes or seconds), you can perform the following calculations. Total minutes between two times: To calculate the minutes between two times, multiply the time difference by 1440, which is the number of minutes in one day (24 hours * 60 minutes = 1440).

How do I calculate the difference between two dates and minutes in Excel?

The following formula can help us compute the difference between two date times in days, hours, and minutes if we have two lists of dates and times. We need to type the following formula into a blank cell: =INT(B2-A2)&" days "&TEXT(B2-A2,"h"" hours ""m"" minutes """)

How do you calculate difference in seconds?

Discussion: If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.


1 Answers

Just use the subtraction operator, and use the Duration method to get the absolute value

DateTime dt1 = ...;
DateTime dt2 = ...;

TimeSpan diff = (dt2 - dt1).Duration();

if (diff.TotalMinutes < 5)
{
    // do something
}
like image 103
Thomas Levesque Avatar answered Oct 23 '22 06:10

Thomas Levesque