Shortening a DateTime Variable

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.)

DateTime OilServiceDate = Convert.ToDateTime(OilServiceDateField).ToShortDateString();
You are casting to a string but your OilServiceDate is a DateTime type

It should be :

string OilServiceDate = Convert.ToDateTime(OilServiceDateField).ToShortDateString();

Also trying to just cast the datetime to string doesn’t change the fact that OilServiceDate is a DateTime Type


you will always need to store it into a string

var newDateString = OilServiceDate.ToShortDateString();

This has been a frustrating process, but I finally have something that works! Since my computer wasn’t acting right I had to go in a different direction. Thank you all for your help! If anyone else is pulling their hair out over something similar, this is what I did.

string result="1/11/2017 12:00:00 AM"; Just an example variable.
string[] Separate = result.Split (' ');
		string OilServiceDate = Separate [0];
		Debug.Log (OilServiceDate);

I split the string at the first space, thus putting both the date and time in separate array elements.
Then I set my variable to the first element in the array.
Thank you all of you for trying to help!

Try parser with mask

DateTime myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",

Could you not just do:

string stringifiedDate = String.Format ("{MM/dd/yy}", OilServiceDate);