- Home /
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.)
$$anonymous$$aybe I should put it a different way. How do you change a DateTime variable to a Short Date Type?
Answer by Gilead7 · Jul 26, 2017 at 07:19 PM
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!
Answer by ShadyProductions · Jul 25, 2017 at 08:00 PM
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
OilServiceDate.ToShortDateString();
you will always need to store it into a string
var newDateString = OilServiceDate.ToShortDateString();
@ShadyProductions The first option gave me the error- cannot convert string to DateTime
The secondoption: FormatException: String was not recognized as a valid DateTime.
The third option: System.String does not contain a definition for ToShortDateString()
Any other ideas? Seems the only way I can make ToShortDateString to work is in a debug, which only changes how it display, not the value of the variable.
The first line is your line and it's there as reference. "ToShortDateString" is a method of the DateTime class and it returns a string however your "OilServiceDate" variable is declared as DateTime and not as string that's why the compiler tells you it can't convert the string (that ToShortDateString returns) into a DateTime object.
So the correct line is the second and this code seems to compile for you, right? However you seem to get a runtime exception which is something completele different from a compiler error. The error message consists of proper english:
FormatException: String was not recognized as a valid DateTime
That means whatever you have in that "OilServiceDateField" string does not represent a date time format that is recognised. If you have a custom date-time string format you may have another look at the answer @DavoBlackOut posted.
Anyways if you want any further help you should include what string you actually pass in as input. Also when you get a compiler error you should post the exact code snippet that you're using and the exact error message. We have the luxus that compilers nowadays talk in proper english to us, so listen to what it's telling you. In the past you may have gotten an error number or simply "failed" / "error" and it was up to you to figure out what you did wrong and where.
Answer by DavoBlackOut · Jul 25, 2017 at 06:47 PM
Try parser with mask
DateTime myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",
System.Globalization.CultureInfo.InvariantCulture);
I don't quite understand. Does the variable to be changed go in first? I've seen this kind of thing before and unity didn't like the line comma other line.
Answer by Kishotta · Jul 25, 2017 at 06:59 PM
Could you not just do:
string stringifiedDate = String.Format ("{MM/dd/yy}", OilServiceDate);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to call time function once time? 0 Answers
Problem with GetNetworkTime 0 Answers