简体   繁体   中英

Function DateDiff is not working in c#.net

I am creating simples days calculation system using C#. I attached code and screen shot below. If I select the date from date and to date we need to calculate days between from date to to date to display on label control.

在此处输入图片说明

This is the code I tried:

private void Button1_Click(object sender, EventArgs e)
{
    int x = DateAndTime.DateDiff(DateInterval.Day, DateTimePicker1.Value.Date, DateTimePicker2.Value.Date);
}

DateDiff is not working

要计算两个日期的差:

double Days = (DateTimePicker2.Value.Date - DateTimePicker1.Value.Date).TotalDays;

this is how you can get total days between two dates.

DateTime date1 = DateTimePicker1.Value.Date;
DateTime date2 = DateTimePicker2.Value.Date;
int daysDiff = ((TimeSpan) (date2 - date1)).Days;  

in short:

int daysDiff = ((TimeSpan) (DateTimePicker2.Value.Date- DateTimePicker1.Value.Date)).Days;  

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