Create DateTime from string without applying timezone or daylight savings
Asked Answered
A

7

16

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.

Amadus answered 20/5, 2011 at 15:0 Comment(1)
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?Benedikta
D
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
  );
Dice answered 20/5, 2011 at 15:9 Comment(0)
D
17

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.

Dhiman answered 20/5, 2011 at 15:5 Comment(2)
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?Amadus
@Journeyman, just access the DateTime property of the DateTimeOffset variable: dtVar = dtoffsetVar.DateTimeKowal
P
8

@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

Philis answered 28/11, 2017 at 6:24 Comment(0)
S
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
Shoon answered 20/5, 2011 at 15:6 Comment(0)
K
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();
Kerrykersey answered 28/7, 2015 at 5:34 Comment(0)
B
0

DateTime.Parse() or DateTime.TryParse()

var clientsideProfileSyncStamp = DateTime.Parse("20-May-2011 15:20:00");
Benedikta answered 20/5, 2011 at 15:2 Comment(0)
S
-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.

Shalon answered 27/5, 2022 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.