简体   繁体   中英

Cannot perform subtraction between System.DateTime and System.DateTime

In a radGridView , I have 3 columns with the type 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.

"Cannot perform '-' operation on System.DateTime and 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

i am using like this for date diff column, it would be better option by database.. DATEDIFF(day,firstdate,lastdate) AS DiffDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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