You must Sign In to post a response.
  • Category: ASP.NET

    Error in finding difference in two dates

    hi friends,

    DateTime dt1 = DateTime.Parse(Convert.ToDateTime(lblarivaldate.Text).ToString("dd/MM/yyyy"));
    DateTime dt2 = DateTime.Parse(Convert.ToDateTime(lbldepdate.Text).ToString("dd/MM/yyyy"), null);
    TimeSpan ts = dt2.Subtract(dt1); //** I got error here**??
    int days = ts.Days;

    error :String was not recognized as a valid DateTime.
  • #765522
    Hi,
    The format string you have in your code is "dd/MM/yyyy" which requires user to enter the date as "18/10/2016", if user enters date in any other format then it will give you that error.
    Use a calendar control
    Or
    Use an array of format strings:
    string[] dateFormats = {
    "dd/MM/yyyy",
    "MM/dd/yyyy",
    "dd-MM-yyyy hh:mm:ss tt",
    "dd-MM-yyyy h:mm:ss tt",
    "dd-MM-yyyy h:m:ss tt",
    "dd-MM-yyyy HH:mm:ss",
    "dd-MM-yyyy H:mm:ss",
    "dd-MM-yyyy H:m:ss",
    "MM/dd/yyyy hh:mm:ss tt",
    "MM/dd/yyyy h:mm:ss tt",
    "MM/dd/yyyy h:m:ss tt",
    "MM/dd/yyyy HH:mm:ss",
    "MM/dd/yyyy H:mm:ss",
    "MM/dd/yyyy H:m:ss"
    };
    DateTime dt1 = DateTime.ParseExact(lblarivaldate.Text, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);//25/11/2013
    DateTime dt2 = DateTime.ParseExact(lbldepdate.Text, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);//11/25/2014
    TimeSpan ts = dt2.Subtract(dt1);
    int days = ts.Days;
    Hope it helps.!

  • #765526
    hi shashi sir,
    Thank u for responding. i'am check i'am my code.it accept only 1 to 12 dates only.not accepted out of 12-31 dates.
    please check.
    Tahnks and Regards

  • #765530
    Hi sadiq,

    As per error details "error :String was not recognized as a valid DateTime", if you are trying to convert string into datetime format, but below string is not a valid datetime format that time you will get this type of error.

    Request you to debug your code and check in which line you got this error and check the string, whether it is valid datetime format or not.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765538
    Hi Sadiq,

    Restrict the user to enter date in specific format because you are not using any datepicker control.So there might be chances of entering date in wrong format.

    Please refer below code:

    string dateString =lblarivaldate.Text;
    string dateString2 =lbldepdate.Text;
    if (!string.IsNullOrEmpty(dateString))
    {
    DateTime date = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat);
    }

    if (!string.IsNullOrEmpty(dateString2))
    {
    DateTime date2 = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat);
    }

  • #765549
    hi friends,
    Please read this question carefully.i'm using datepicker.
    i'am checking my code.it accept 1 to 12 dates only.not accepted 13-31 dates.
    because it gives result "1 to 12 dates" dosn't given other dates.
    Tahnks and Regards

  • #765550
    Hi sadiq,

    Why it is accepted 1-12 dates only, why it won't accept the dates in between 13-31?
    As per information provided by you it won't say the date restriction, we can't give you solution as per your code block provided by you.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765551
    'String was not recognized as a valid DateTime' error indicates your have got wrong data in date object which is not converted to DateTime.
    Use below method to convert string to dateTime

    string dt = "24/12/2010";
    DateTime dt1;
    //always use InvariantCulture for dateformat provider as it support large number of customized datetime formats
    System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
    dt1 = DateTime.ParseExact(dt, "dd/MM/yyyy", provider);
    MessageBox.Show("String successfully converted to DateTime" + dt1.Year);

    then you can use below code to get the difference between date in Days

    (EndDate - StartDate).TotalDays

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765554
    hi Prasad kulkarni sir,
    Thank you for responding.
    i'am trying your code but it shows same error.i'am not understand what is going on please help me.
    Thank and Regards
    sadiq,


Sign In to post your comments