Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gilead7 · Jul 25, 2017 at 06:35 PM · c#datetimeshort

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gilead7 · Jul 25, 2017 at 11:05 PM 0
Share

$$anonymous$$aybe I should put it a different way. How do you change a DateTime variable to a Short Date Type?

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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();
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gilead7 · Jul 25, 2017 at 11:04 PM 0
Share

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

avatar image Bunny83 Gilead7 · Jul 26, 2017 at 01:29 AM 0
Share

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.

avatar image Gilead7 · Jul 26, 2017 at 03:46 PM 0
Share

@Bunny83 Thanks!

avatar image
-1

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


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gilead7 · Jul 25, 2017 at 07:03 PM 0
Share

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.

avatar image
-1

Answer by Kishotta · Jul 25, 2017 at 06:59 PM

Could you not just do:

 string stringifiedDate = String.Format ("{MM/dd/yy}", OilServiceDate);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gilead7 · Jul 25, 2017 at 11:10 PM 0
Share

Input string not in correct format...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

360 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges