I was reading into answerer's post here where I ran into this enumerator
The DateTimeKind field of a date is preserved when a DateTime object
is converted to a string using the "o" or "r" standard format
specifier, and the string is then converted back to a DateTime object.
So I was finally able to understand this and sharing the same information here if it can be helpful for others too:
First part is conversion of C# DateTime object into string. There are many format specifiers to do that but for us "r" and "o" format specifiers are of concern to us with regards to
DateTimeStyles.RoundtripKind. You can see all date time format specifiers here. See what happens when we do the conversion in code using these format specifiers:
//r corresponds to RFC 1123 format (GMT date time format) var gmtDateTimeString = DateTime.Now.ToString("r"); //gives Fri, 23 Sep 2016 15:39:21 GMT //o corresponds to ISO 8601 (Local date time format) var localDateTimeString = DateTime.Now.ToString("o"); //gives 2016-09-23T15:39:21.8899216+05:30
You can clearly see that string date time being output has the information embedded inside it which suggests:
Fri, 23 Sep 2016 15:39:21 GMTis of
DateTimeKind.Utc("GMT" text is present)
2016-09-23T15:39:21.8899216+05:30represents a date time of
DateTimeKind.Local("T" character is present as per
Now comes the second part. If I've to convert these date time strings
localDateTimeString back to a date time object then we need to parse them. So with the help of
DateTimeStyles.RoundtripKind enumeration value passed to
DateTime.Parse API you actually signify that time zone information is already baked in the string and API parses the date time appropriately using that information.
Normally when date time data is transferred over the wire in XML format then ISO 8601 format is used which I saw in the post which I referred before posting the question in this thread. So while parsing such a date time string obtained from an XML document it was appropriate to use the
DateTimeStyles.RoundtripKind to get the right date time value as per the time-zone information present in the string.