Fernando
Fernando

Reputation: 358

date difference between two dates

I want to calculate the date difference between form date and two date..Am using timespan to calculate the difference between two date if date difference is positive means it enter another process falls means it return error message.

My partial code is here..

  TimeSpan span = Convert.ToDateTime(txtenddate.Text).Subtract(Convert.ToDateTime(txtstartdate.Text));
        int formatted = span.Days;
        if (formatted < 1)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Invalid date difference ');</script>", false);
        } 

In the above code input is end date : 30-01-2004 start date : 01-02-2002

but it returns error message : String was not recognized as a valid DateTime.

please give me a solution to solve this with out changing date format...

Upvotes: 0

Views: 1676

Answers (5)

Sujatha Girijala
Sujatha Girijala

Reputation: 1209

for days difference

  public long getDaysBetweenDates(Date d1, Date d2){
return TimeUnit.MILLISECONDS.toDays(d1.getTime() - d2.getTime());
}

Date difference between days with time

Date startDate = // Set start date
Date endDate   = // Set end date

long duration  = endDate.getTime() - startDate.getTime();

long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);

Upvotes: 0

Zoli
Zoli

Reputation: 250

You must use CultureInfo maybe the default CultureInfo different from "en-GB";

var cult = new System.Globalization.CultureInfo("en-GB");
TimeSpan span = Convert.ToDateTime("30-01-2004", cult).Subtract(Convert.ToDateTime("01-02-2002", cult));

Upvotes: 1

shubhangi Motling
shubhangi Motling

Reputation: 292

Your dateformat should be like this. StartDate=1/2/2002 and EndDate=3/1/2004

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38210

You should be using ParseExact to get the relevant DateTime.

TimeSpan ts = DateTime.ParseExact("30-01-2004", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture)
                  - DateTime.ParseExact("01-02-2002", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);

While using Convert.ToDateTime it invokes DateTime.Parse which will try the conversion corresponding to the your current culture setting which as in this case doesn't support the format of DateTime you have, so you should rely on the ParseExact whereby you know the format in which the string is expected and achieve in fetching your result.

Upvotes: 2

Jeff S
Jeff S

Reputation: 7484

You need to specify the culture for the conversion. By default, it should be using the default date format for your PC but this doesn't always work.

You should take a look at this about specifying a format provider for the Convert.ToDateTime method http://msdn.microsoft.com/en-us/library/9xk1h71t.aspx

and this about the DateTimeFormatInfo object you will need to create to handle the culture: http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx

Upvotes: 1

Related Questions