With the code below, I'm trying to convert this datetime
string to a Local DateTime
private DateTime ConvertToLocalTime(string datetimestring)
{
DateTime timeUtc = DateTime.Parse(datetimestring);
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
return cstTime;
}
This is the Exception I'm getting:
at System.TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone, TimeZoneInfoOptions flags, CachedData cachedData) at
System.TimeZoneInfo.ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo destinationTimeZone)
The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly. For example, when the Kind property is
DateTimeKind.Local
, the source time zone must be TimeZoneInfo.Local.
The example for ConvertTimeFromUtc looks exactly like my code except I'm parsing this string into the timeUtc:
2017-01-23T05:00:00+00:00
If I call the Parse like this:
DateTime.Parse(datetimestring, null, System.Globalization.DateTimeStyles.RoundtripKind);
timeUtc.Kind.ToString()
returns "Local"
So, how do I remedy this? The times are going to be sent to me UTC.