We are developing a C# application for a web-service client. This will run on Windows XP PC\'s.
One of the fields returned by the web service is a DateTime field. Th
For strings such as 2012-09-19 01:27:30.000
, DateTime.Parse
cannot tell what time zone the date and time are from.
DateTime
has a Kind property, which can have one of three time zone options:
NOTE If you are wishing to represent a date/time other than UTC or your local time zone, then you should use DateTimeOffset.
So for the code in your question:
DateTime convertedDate = DateTime.Parse(dateStr);
var kind = convertedDate.Kind; // will equal DateTimeKind.Unspecified
You say you know what kind it is, so tell it.
DateTime convertedDate = DateTime.SpecifyKind(
DateTime.Parse(dateStr),
DateTimeKind.Utc);
var kind = convertedDate.Kind; // will equal DateTimeKind.Utc
Now, once the system knows its in UTC time, you can just call ToLocalTime
:
DateTime dt = convertedDate.ToLocalTime();
This will give you the result you require.