- Home /
How to display Json DateTime in Unity?,How to display Json DateTime in unity?
Unable to get Json DateTime in unity using JsonUtility serialize/deserialize. What are the workarounds to get DateTime? TIA,Hi, I'm new to Unity. Trying to get json DateTime to Unity. JsonUtility deserialize is not working and giving 01-01-0001. Any help can be appreciated!
Answer by Bunny83 · Jun 19, 2020 at 01:39 AM
JSON is a simple serialization format with only a handful of data types. Specifically "Objects", "Arrays", "Numbers", "Strings", "Boolean" and the special value "null". Any other data type you want to store as JSON has to be encoded into one of the supported formats. So DateTime does not have any representation in JSON.
Apart from that Unity's serialization system does not support the datatype "DateTime". So the JsonUtility does not serialize DateTime values at all.
You said that you're mainly interested in deserializing DateTime values. In what format are those actually stored in your JSON? Please be more precise when asking questions. It's also not clear what you mean by "display"
Hello @Bunny83 thanks for the immediate response. Sorry, Here is the format I'm looking to read JSON Datetime(World timezone) to Unity. Date":"2020-06-18T14:36:18Z". Currently with JsonUtility I'm getting output in console is Date: 0001-01-01T00:01:SSZ. Thanks!
Well, that string should be able to be parsed by the DateTime.Parse method without any issues. You still haven't shown your code so we can not tell you what you did wrong. Again you should stop calling it a "json date time" since json does not have such a datatype. You can store a datetime value in a json string and read that string back and after that parse it through DateTime.Parse. If you do that it should work just fine.
Thanks @Bunny83 ! DateTime.Parse method working fine.