C# 如何像UTC那样将字符串转换为DateTime就这么简单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13254211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:56:58  来源:igfitidea点击:

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

c#datetimedatetimeoffset

提问by user1025852

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?

假设我有这个字符串:如何将其转换为具有 UTC 时间的 DateTimeOffset 对象 - 意味着 -00:00 作为时区 - 即使我在特定时区的机器上运行它?

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

假设字符串:“2012-10-08T04:50:12.0000000”

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

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)

--> DateTime d = {10/8/2012 6:50:12 AM} 我希望它是 DateTime d = {10/8/2012 4:50:12 AM}好像它会理解我想要日期就这么简单(顺便说一句 - 我的机器在时区 +02:00)

采纳答案by Kna?is

Use DateTimeOffset.Parse(string).UtcDateTime.

使用DateTimeOffset.Parse(string).UtcDateTime.