Nisa Yathu
Nisa Yathu

Reputation: 39

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.

enter image description here

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

Upvotes: 0

Views: 3655

Answers (2)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34160

To Calculate the Difference of two Dates:

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

Upvotes: 1

Nomi Ali
Nomi Ali

Reputation: 2252

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;  

Upvotes: 0

Related Questions