how to convert string to DateTime as UTC as simple as that
Asked Answered
P

5

32

assume I have this string : How can I convert it to DateTimeOffset object that will have UTC time - means -00:00 as Time Zone - even if I run it on machine on a specific timezone?

Assume String: "2012-10-08T04:50:12.0000000"

Convert.ToDateTime("2012-10-08T04:50:12.0000000" + "Z");

--> DateTime d = {10/8/2012 6:50:12 AM} and I want it to be DateTime d = {10/8/2012 4:50:12 AM} as if it will understand I want the date as simple as it comes (BTW - my machine is in timezone +02:00)

Point answered 6/11, 2012 at 15:28 Comment(1)
so do you want DateTime or DateTimeOffset?Brouhaha
I
87

Use DateTimeOffset.Parse(string).UtcDateTime.

Instantaneous answered 6/11, 2012 at 15:32 Comment(1)
Do not know if something has changed in .NET since this was asked but I had to append ToString() to the end to get this to work, but it works perfectly DateTimeOffset.Parse(string).UtcDateTime.ToString()Adumbrate
B
9

The accepted answer did not work for me. Using DateTimeOffset.Parse(string) or DateTimeOffset.ParseExact(string) with the .UtcDateTime converter correctly changed the kind of the DateTime to UTC, but also converted the time.

To get to a DateTime that has the same time as the original string time, but in UTC use the following:

DateTime dt = DateTime.ParseExact(string, "yyyy-MM-ddTHH:mm:ss.fffffff",
    CultureInfo.InvariantCulture);
dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
Brabazon answered 29/11, 2020 at 5:37 Comment(0)
S
2
var universalDateTime = DateTime.Parse(your_date_time_string).ToUniversalTime();
Selfidentity answered 17/12, 2020 at 14:32 Comment(1)
how do you reverse the universalDataTime back into a string? 926291224800000 given this valueChild
P
1

I did this by checking the DateTimeKind. On my function, 2 different types of date-times are coming. What I want is to convert UTC time to local time from the below function. Input parameter date is always coming as UTC.

Eg inputs: 2021-01-19 07:43:00 AM and 01/07/2021 02:16:00 PM +00:00

public static DateTime GetDateTime(string date)
    {
        try
        {
            DateTime parsedDate = DateTime.Parse(date, GetCulture()); //invarient culture

            if (parsedDate.Kind == DateTimeKind.Unspecified)
            {
                parsedDate = DateTime.SpecifyKind(parsedDate, DateTimeKind.Utc);
            }
            
            return parsedDate.ToLocalTime();
        }
        catch (Exception e)
        {
            throw;
        }
    }
Piecedyed answered 7/1, 2021 at 14:21 Comment(0)
C
0

Using, DateTimeStyles.AssumeUniversal in DateTime.Parse(...) will do the job,

Ex:

DateTime.Parse("2023-01-02 9:26", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal)

then the parsed datetime will be in UTC

Communistic answered 2/1, 2023 at 6:34 Comment(1)
This takes the string "2023-08-31T23:59:59Z" parses it as local and then applies the offset to convert to UTC.Expiation

© 2022 - 2024 — McMap. All rights reserved.