How to Compare Date Time In C#

This article for know about during of start date and end date.
Example: StartDate= 10/05/2009 and EndDate= 15/05/2009
You want to know about during of 2 date. how do you do?
and you want to know which first and which last.
If you want to do it please see code below:

1. Find during of this date.

DateTime startDate= new DateTime();
DateTime endDate=new DateTime();

startDate=Convert.ToDateTime(txtStartDate.Text);
endDate=Convert.ToDateTime(txtEndDate.Text);

int during= endDate-startDate;

Response.Write(during.ToString());

But this code for find during that in the same month and the same year.
can not calculate in different year and month. But we can compare date time
by using function of C#.

2. Compare Date Time

DateTime startDate= new DateTime();
DateTime endDate=new DateTime();

startDate=Convert.ToDateTime(txtStartDate.Text);
endDate=Convert.ToDateTime(txtEndDate.Text);

int during =endDate.CompareTo(startDate);

if (startDate > endDate)
{
Response.Write("Start Date is big");
}
else
{
Response.Write("Start Date is Small");
}

No comments:

Post a Comment