String to Datetime Doesnt work in Unity but work in ConsoleApp

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…

var time = DateTime.Now.ToString("yyMMddHHmmssmm");
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:

System.FormatException: Invalid format string
  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
  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
  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
  at Assets.Tactical.Scripts.UI.LoadMenu.LoadData () [0x00035] in E:\GameProject\Tactical\Tactical\Assets\Tactical\Scripts\UI\LoadMenu.cs:36
UnityEngine.Debug:Log(Object)
Assets.Tactical.Scripts.UI.LoadMenu:LoadData() (at Assets/Tactical/Scripts/UI/LoadMenu.cs:49)
Assets.Tactical.Scripts.UI.LoadMenu:Awake() (at Assets/Tactical/Scripts/UI/LoadMenu.cs:20)

EDIT:

I also tried with CultureInfo.CurrentUICulture

var strFormat = "yyMMddHHmmssmm";
var time = DateTime.Now.ToString(strFormat, CultureInfo.CurrentUICulture);
var date = DateTime.ParseExact(time, strFormat, CultureInfo.CurrentUICulture);

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.

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:

var date = DateTime.Now.ToString(CultureInfo.InvariantCulture);
date = date.Replace('/', '-');
date = date.Replace(':', '_');

Converting String To Date

dateStr = dateStr.Replace('_', ':');
dateStr = dateStr.Replace('-', '/');
var date = Convert.ToDateTime(dateStr);

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.

You sir, are a gentleman and a scholar!

Note, in the current version of Unity (2017 and newer) where you can target .Net 4.6 you can now use the format strings.