How to convert String To DateTime In Unity C#

How to fetch Day, Month or year from date time string?
How to get Hour, minute or second from a date time string?

Following snippet of code can be used to deal with most of the forms of date time string in Unity C#
Instead of having time in the form of
  "2020-1-31 12:10:15" 
you can also pass
"31/1/2020 12:10:15 PM"

and or any such case of Date time in string.
Then from this DateTime object you can fetch any particular detail like Year shown in example below.


System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
DateTime tempDate = Convert.ToDateTime("2020-5-6 12:10:15", culture);
Debug.Log("Testing date time conversion: " + tempDate.Year);



This could be used to deal with date time strings of the form 
 "dd:MM:yyyy HH:mm:ss"
"yyyy:MM:dd HH:mm:ss"

Comments