且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中找出两个日期时间之间的差异

更新时间:2023-02-07 18:35:15

找到区别是琐碎:

 DateTime dt1,dt2; 
...
TimeSpan diff = td2 - dt1;

Timespan结构可以用来评估差异的几个区间:TimeSpan Structure(System) [ ^ ]

但是作为F-ES Sitecore如果你的数据库中的值是DATE或DATETIME列,你应该直接使用它们:

 DateTime dtAlert =(DateTime)行[  AlertsendTime]; 
DateTime dtSend =(DateTime)rows [ DateTimeSend];
TimeSpan diff = dtSend - dtAlert;



如果这不起作用,则表示您的数据库出现问题,最可能的原因是列是VARCHAR或NVARCHAR - 在这种情况下,日期信息的格式是错误的(例如MM / DD / YY而不是DD / MM / YY)或者只是错误的。因此,首先使用调试器来查看数据库返回的确切内容,并查看您可以找到的内容。

我们无法为您执行此操作 - 我们无法访问您的数据!


如果所有其他方法都失败了,那么你就做了自计算机早期以来每个人都做过的事情。把日期改为朱利安,把时间改为24小时。这是实现它的股票标准方式。



该算法称为CACM算法199

 / * -------------------------------------------------- ------------------------- 
JulianDay返回给定日期的Julian日期值。
通常用于计算两个实际日期之间的天数
根据http://aa.usno.navy.mil/data/docs/JulianDate.html检查
12Oct06 LdB CACM算法199 - 一天从中午开始
------------------------------------------ -------------------------------- * /
unsigned long JulianDay(unsigned short Day,unsigned short Month, unsigned short Year){
long Tmp1,Tmp2,Tmp3,Tmp4;

if(Month> 2){//超过2月
Tmp1 = Month - 3; //临时值1
Tmp2 =年份; //临时值2
}其他{
Tmp1 =月+ 9; //临时值1
Tmp2 =年 - 1; //临时值2
}
Tmp3 = Tmp2 / 100; //临时值3
Tmp3 =(Tmp3 * 146097)/ 4;
Tmp4 = Tmp2%100; //临时值4
Tmp4 =(Tmp4 * 1461)/ 4;
Tmp1 =(153 * Tmp1 + 2)/ 5;
返回(Tmp3 + Tmp4 + Tmp1 + Day + 1721119); //返回朱利安日
}


简单的一行解决方案但两者都应该是DateTime类型



 DateTime.Now.Subtract(yourDateTimevalue).TotalMinutes >   0 跨度> 


I have two datetime variable coming from database and have to find difference between two datetime using if loop.

What I have tried:

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



but it cannot compare datetime

Finding the difference is trivial:
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:

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!


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
}


Simple one line solution but both should be of type DateTime

DateTime.Now.Subtract(yourDateTimevalue).TotalMinutes > 0