how to convert string to DateTime as UTC as simple as that

后端 未结 3 943
南方客
南方客 2020-12-31 05:29

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 ti

3条回答
  •  别那么骄傲
    2020-12-31 06:02

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

提交回复
热议问题