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…
You should be using
ParseExactto get the relevantDateTime.While using
Convert.ToDateTimeit invokesDateTime.Parsewhich will try the conversion corresponding to the your current culture setting which as in this case doesn’t support the format ofDateTimeyou have, so you should rely on theParseExactwhereby you know the format in which the string is expected and achieve in fetching your result.