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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

String to Datetime Doesnt work in Unity but work in ConsoleApp

Discussion in 'Scripting' started by Korigoth, Oct 8, 2015.

  1. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    Hey,

    i tried to parse a Datetime on my game save file, so i can show the date to the player without loading everything.

    but unity always fail to to the job... telling i have a bad format... but it's not true at all.... because it work perfectly on a fresh console app...

    Code (CSharp):
    1. var time = DateTime.Now.ToString("yyMMddHHmmssmm");
    2. var date = DateTime.ParseExact(time, "yyMMddHHmmssmm", null);
    this code work on Console.
    Fail in Unity!? why

    and how could i fix this... i dont really want to write a string analyser and parse it by myself... when the tool exist and it's working usually?!

    here is the stack trace:
    Code (CSharp):
    1. System.FormatException: Invalid format string
    2.   at System.DateTime.ParseExact (System.String s, System.String[] formats, IFormatProvider provider, DateTimeStyles style) [0x0005c] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:1892
    3.   at System.DateTime.ParseExact (System.String s, System.String format, IFormatProvider provider, DateTimeStyles style) [0x0001c] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:1870
    4.   at System.DateTime.ParseExact (System.String s, System.String format, IFormatProvider provider) [0x00000] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:1030
    5.   at Assets.Tactical.Scripts.UI.LoadMenu.LoadData () [0x00035] in E:\GameProject\Tactical\Tactical\Assets\Tactical\Scripts\UI\LoadMenu.cs:36
    6. UnityEngine.Debug:Log(Object)
    7. Assets.Tactical.Scripts.UI.LoadMenu:LoadData() (at Assets/Tactical/Scripts/UI/LoadMenu.cs:49)
    8. Assets.Tactical.Scripts.UI.LoadMenu:Awake() (at Assets/Tactical/Scripts/UI/LoadMenu.cs:20)

    EDIT:

    I also tried with CultureInfo.CurrentUICulture

    Code (CSharp):
    1. var strFormat = "yyMMddHHmmssmm";
    2. var time = DateTime.Now.ToString(strFormat, CultureInfo.CurrentUICulture);
    3. var date = DateTime.ParseExact(time, strFormat, CultureInfo.CurrentUICulture);
     
    Last edited: Oct 8, 2015
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yeah, I tried this recently as well, and it turns out the version of mono that is used is older than the .net inclusion of DateTime format strings.

    So yeah, no support for this, sorry.
     
  3. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    I got a wierd solution but it actually work.... when does Mono will become a little bit more up-to-date?!

    Solution:
    Parsing Date to String:
    Code (CSharp):
    1. var date = DateTime.Now.ToString(CultureInfo.InvariantCulture);
    2. date = date.Replace('/', '-');
    3. date = date.Replace(':', '_');
    Converting String To Date
    Code (CSharp):
    1. dateStr = dateStr.Replace('_', ':');
    2. dateStr = dateStr.Replace('-', '/');
    3. var date = Convert.ToDateTime(dateStr);
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    The latest version of mono does support it.

    Unity doesn't use the latest version of mono, and probably won't be for a long while.
     
  5. Ace71425

    Ace71425

    Joined:
    Oct 8, 2017
    Posts:
    2

    You sir, are a gentleman and a scholar!
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Note, in the current version of Unity (2017 and newer) where you can target .Net 4.6 you can now use the format strings.