36

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)

1
  • 2
    so do you want DateTime or DateTimeOffset? Commented Nov 6, 2012 at 15:31

5 Answers 5

93

Use DateTimeOffset.Parse(string).UtcDateTime.

Sign up to request clarification or add additional context in comments.

1 Comment

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()
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);

Comments

2
var universalDateTime = DateTime.Parse(your_date_time_string).ToUniversalTime();

1 Comment

how do you reverse the universalDataTime back into a string? 926291224800000 given this value
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;
        }
    }

Comments

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

1 Comment

This takes the string "2023-08-31T23:59:59Z" parses it as local and then applies the offset to convert to UTC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.