Create DateTime from string without applying timezone or daylight savings

Journeyman picture Journeyman · May 20, 2011 · Viewed 18.8k times · Source

How do I create a DateTime var from a string which is already adjusted for UTC? I am running this on a machine set to BST (GMT+1). If I run the following line of code:

DateTime clientsideProfileSyncStamp = Convert.ToDateTime("20-May-2011 15:20:00");

and then use the value in a test against a database holding (UTC) values then it would appear that the Convert.ToDateTime() is actually giving me a UTC value of 14:20. I don't want it to do the conversion - I just want it to accept that my DateTime string is already in UTC.

Thanks.

Answer

Guffa picture Guffa · May 20, 2011

Parse the string, and specify that it should assume UTC time when there is no time zone specified in the string:

DateTime clientsideProfileSyncStamp =
  DateTime.Parse(
    "20-May-2011 15:20:00",
    CultureInfo.CurrentCulture,
    DateTimeStyles.AssumeUniversal
  );