Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

String Was Not Recognized As A Valid DateTime

Discussion in 'Scripting' started by gaglabs, Mar 13, 2020.

  1. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Iv'e been trying to get a final year from subtracting two dates and getting a total year timespan. Below is my code. It keeps throwing an error as described in the title.

    Code (CSharp):
    1.  
    2.             int _thisYear = DateTime.Now.Year;
    3.             int _thisDay = DateTime.Now.Day;
    4.             int _thisMonth = DateTime.Now.Month;
    5.                                  
    6.                                  
    7.             string ThisDate = _thisDay + "/" + _thisMonth + "/" + _thisYear;
    8.             string BDay = _userDay + "/" + _bdayMonth + "/" + _userYear;
    9.             Debug.Log("this date is" + " " + ThisDate);
    10.             Debug.Log("bday is" + " " + BDay);
    11.          
    12.              //Throws error here
    13.             DateTime dateToday = System.DateTime.ParseExact(ThisDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
    14.              DateTime dateBday = System.DateTime.ParseExact(BDay, "dd/MM/yyyy", CultureInfo.InvariantCulture);
    15.                                  
    16.                                  
    17.                TimeSpan _finalDates = dateToday - dateBday;
    18.                Age.text = _finalDates.ToString();
    19.  
    20.  
    21.  
    My debugs return this : this date is 13/3/2020 & bday is 10/7/1979
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Those strings don't match your format string, the month is missing a digit:
    dd/MM/yyyy
    13/3?/2020
    10/7?/1979
    You need to add leading zeros to the month to make it match
    13/03/2020
    10/07/1979
     
  3. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    Yeah that still did nothing. I fixed it by this..

    Code (CSharp):
    1.  
    2.        int _thisYear = DateTime.Now.Year;
    3.        int _thisDay = DateTime.Now.Day;
    4.        int _thisMonth = DateTime.Now.Month;
    5.        int _thisHour = DateTime.Now.Hour;
    6.        int day = Convert.ToInt32(_userDay);
    7.        int year = Convert.ToInt32(_userYear);
    8.        DateTime fromTrimmed = new DateTime(_thisYear, _thisMonth, _thisDay, 0, 0, 0);
    9.        DateTime toTrimmed = new DateTime(year, _bdayMonth, day, 0, 0, 0);
    10.  
    11.         string final = (fromTrimmed - toTrimmed).TotalDays.ToString();
    12.  
    13.         int FinalInt = Convert.ToInt32(final);
    14.  
    15.          Age.text = (FinalInt / 365).ToString() + " " + "Yrs Old";
     
  4. malcomjarr

    malcomjarr

    Joined:
    Nov 18, 2020
    Posts:
    7
    Use DateTime.ParseExact.- Converts the specified string representation of a date and time to its C# Dateime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. ParseExact and TryParseExact allows to use a custom format string.

    Code (CSharp):
    1. DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
    The IFormatProvider parameter specifies the culture to use to parse the date. Unless your string comes from the user, you should pass CultureInfo.InvariantCulture. If the string does come from the user, you should pass CultureInfo.CurrentCulture, which will use the settings that the user specified in Regional Options in Control Panel.