Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have two datetime variable coming from database and have to find difference between two datetime using if loop.

What I have tried:

C#
if (flag == "P" && (Convert.ToDateTime(rows["AlertsendTime"].ToString())  Convert.ToDateTime(rows["DateTimeSend"].ToString())).TotalMinutes > 5)


but it cannot compare datetime
Posted
Updated 30-May-16 21:22pm
v2
Comments
F-ES Sitecore 25-May-16 7:19am    
First off what data types are AlertsendTime and DateTimeSend? If they are Date then don't ToString them in your code, instead cast them to DateTime<br abp="745"><br abp="746">((DateTime)rows["AlertSendTime"])
Karthik_Mahalingam 25-May-16 7:20am    
what is the value in rows["AlertsendTime"] and rows["DatesendTime"]

Finding the difference is trivial:
C#
DateTime dt1, dt2;
...
TimeSpan diff = td2 - dt1;
The Timespan struct as several intervals you can use to evaluate the difference: TimeSpan Structure (System)[^]
But as F-ES Sitecore says, if the values in your DB are DATE or DATETIME columns, you should just use them directly:
C#
DateTime dtAlert = (DateTime) rows["AlertsendTime"];
DateTime dtSend = (DateTime) rows["DateTimeSend"];
TimeSpan diff = dtSend - dtAlert;

If that doesn't work, it implies that you have a problem in your DB, and the most likely reason is that the columns are VARCHAR or NVARCHAR - in which case the date information is either in the wrong format (MM/DD/YY instead of DD/MM/YY for example) or just plain wrong. So start by using the debugger to look at exactly what the DB is returning, and see what you can find.
We can't do that for you - we don't have access to your data!
 
Share this answer
 
If all else fails you do what everyone has always done since the early days of computers .. turn the dates to Julian and the time to 24 hour. It's sort of the stock standard way to do it.

The algorithm is called CACM Algorithm 199
/*---------------------------------------------------------------------------
  JulianDay	 Returns the Julian day value of the given date.
  Commonly used to calculate number of days between two actual dates
  checked against http://aa.usno.navy.mil/data/docs/JulianDate.html
  12Oct06 LdB	CACM Algorithm 199 - day begins at noon
  --------------------------------------------------------------------------*/
unsigned long JulianDay (unsigned short Day, unsigned short Month, unsigned short Year){
	long Tmp1, Tmp2, Tmp3, Tmp4;

	if (Month > 2){													// Beyond february
		Tmp1 = Month - 3;											// Temp value 1
		Tmp2 = Year;												// Temp value 2
	} else {
		Tmp1 = Month + 9;											// Temp value 1
		Tmp2 = Year - 1;											// Temp value 2
	}
	Tmp3 = Tmp2 / 100;												// Temp value 3
	Tmp3 = (Tmp3 * 146097) / 4;
	Tmp4 = Tmp2 % 100;												// Temp value 4
	Tmp4 = (Tmp4 * 1461) / 4;
	Tmp1 = (153 * Tmp1 + 2) / 5;
	return (Tmp3 + Tmp4 + Tmp1 + Day + 1721119);					// Return julian day
}
 
Share this answer
 
v3
Simple one line solution but both should be of type DateTime

C#
DateTime.Now.Subtract(yourDateTimevalue).TotalMinutes > 0
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900