简体   繁体   English

无法在System.DateTime和System.DateTime之间执行减法

[英]Cannot perform subtraction between System.DateTime and System.DateTime

In a radGridView , I have 3 columns with the type GridViewDateTimeColumns . radGridView ,我有3列GridViewDateTimeColumns类型。 I need to subtract two of these columns to get the days difference in the third column. 我需要减去其中两列以获得第三列中的天数差异。

 radGridView1.Columns["Ritardo"].Expression = "Periodo-Data_Pagam";

I get this error regardless of coding the above line or writing the expression in the designer. 无论编写上述行还是在设计器中编写表达式,我都会收到此错误。 The data in the columns "Periodo" and "Data_Pagam" are both from SQL2012 DB dateTime type. “Periodo”和“Data_Pagam”列中的数据均来自SQL2012 DB dateTime类型。

"Cannot perform '-' operation on System.DateTime and System.DateTime “无法对System.DateTime和System.DateTime执行' - '操作

How can I get the difference (days) between the two above date. 如何获得上述两个日期之间的差异(天数)。

使用DateDiff(Day,StartDate,Enddate)你可以DateDiff(Day,StartDate,Enddate)这个

If you want day difference by coding then you can use this type of coding also. 如果您想通过编码获得日差,那么您也可以使用这种类型的编码。

DateTime sp1 = DateTime.ParseExact(firstdate, "MMddyyyyHHmm", null);
DateTime ss1 = DateTime.ParseExact(lastdate, "MMddyyyyHHmm", null);
double ts = (ss1 - sp1).Days;

difference will be ts 差异将是ts

i am using like this for date diff column, it would be better option by database.. DATEDIFF(day,firstdate,lastdate) AS DiffDate 我正在使用这样的日期差异列,它将是更好的数据库选项.. DATEDIFF(day,firstdate,lastdate) AS DiffDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM