12

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Journeyman
  • 10,011
  • 16
  • 81
  • 129
  • When I just tried `Convert.ToDateTime()`, it returned the exact value that the string represents. Are you calling `.ToUniversalTime()`? What are you doing with the value after you do the conversion? – NerdFury May 20 '11 at 15:05

7 Answers7

19

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
  );
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
16

Use

DateTimeOffset.Parse

The under-advertised DateTimeOffset type represents a point in time regardless of timezone differences, and as such should be used in preference to DateTime where a 'timestamp' is required.

Matt Howells
  • 40,310
  • 20
  • 83
  • 102
  • 1
    looks good. I need to then pass it to a 3rd-party API that is expecting a std DateTime - what is the safest way to convert it to the DateTime? – Journeyman May 20 '11 at 15:09
  • 2
    @Journeyman, just access the `DateTime` property of the `DateTimeOffset` variable: `dtVar = dtoffsetVar.DateTime` – chiapa May 27 '15 at 16:42
6

@Guffa's answer is very good but i will like to add an additional answer. If your datetime string is looking like this "2017-11-27T05:30:00.000Z" then AssumeUniversal is not working. Try this :

    DateTime.Parse("2017-11-27T05:30:00.000Z", null, System.Globalization.DateTimeStyles.AdjustToUniversal);

There is a slight difference between AssumeUniversal and AdjustToUniversal. Read here : Difference between AssumeUniversal and AdjustToUniversal

2

Don't forget the TryParse variant which allows you to handle a parse error without an exception

DateTime clientsideProfileSyncStamp;
DateTime.TryParse(
    "20-May-2011 15:20:00",
    System.Globalization.CultureInfo.CurrentCulture,
    System.Globalization.DateTimeStyles.AssumeUniversal,
    out clientsideProfileSyncStamp
);

Also if you are not using ParseExact or TryParseExact it will assume the output Kind is Local so you may also want to use ToUniversalTime()

clientsideProfileSyncStamp.ToUniversalTime();
KCD
  • 9,873
  • 5
  • 66
  • 75
2

Add a Z to the DateTime string:

DateTime clientsideProfileSyncStamp = Convert.ToDateTime("20-May-2011 15:20:00Z");
Console.Write(clientsideProfileSyncStamp.ToUniversalTime()); // 20-May-2011 15:20:00
Forgotten Semicolon
  • 13,909
  • 2
  • 51
  • 61
0

DateTime.Parse() or DateTime.TryParse()

var clientsideProfileSyncStamp = DateTime.Parse("20-May-2011 15:20:00");
NerdFury
  • 18,876
  • 5
  • 38
  • 41
-1

To create culture independent DateTime use:

DateTime.Parse("2022-02-15 09:30:47", CultureInfo.InvariantCulture)

Then the date stays always exactly as defined in string.

Marcin
  • 479
  • 6
  • 11