I was having trouble sorting dates that were strings because anytime there was a 1 or 11 in the date, the sort placed it up at the top. I’m working on switching to DateTime to prevent this problem, but I must be missing something. While I can convert the string input to DateTime, to change it to a shortdate gives me a mismatch error- cannot convert Datetime to string. What am I doing wrong?
OilServiceDateField - input taken from the user(string)
DateTime OilServiceDate = Convert.ToDateTime(OilServiceDateField);
Then I convert the string to datetime. Now how do I make the datetime a shortdate?
OilServiceDate.ToShortDateString(); This gives a convert error. Do I need to cast? I tried it all on one line--
DateTime OilServiceDate = Convert.ToDateTime(OilServiceDateField).ToShortDateString(); It gives me the same convert error.
What needs to be done to make it work? Thanks!
The database is saving it as text.
Call me crazy, I think I getting there. I just set up a test script and it worked, brought it back to the script and it didn’t work. Same error Format Excemption FormatException: String was not recognized as a valid DateTime.
System.DateTime.Parse (System.String s, IFormatProvider provider, DateTimeStyles styles) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/DateTime.cs:924)
string ourDate="7/25/2017";
Debug.Log ("String received " +ourDate);
DateTime dateToDisplay = Convert.ToDateTime (ourDate);
Debug.Log ("Before Conversion " + dateToDisplay);
string stringDate = Convert.ToDateTime(dateToDisplay).ToShortDateString();
Debug.Log ("After Shorting" +stringDate);
I checked the type of the variable. It is a string. Somehow when I replace the above code with another string which is equal to a Text.text, it spits out the error.
Any further thoughts? I’m just about to go back to all strings and deal with the sort issue(anytime a 1 or 11 is in the date, it promptly goes to the top of the list rather than where is belongs in the sort.)