如何将字符串转换为像UTC这样的DateTime就这么简单 [英] how to convert string to DateTime as UTC as simple as that

查看:153
本文介绍了如何将字符串转换为像UTC这样的DateTime就这么简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



-> DateTime d = {2012/10/8 6:50:12}
,我希望它成为
DateTime d = {10/8/2012 4:50:12 AM} ,好像可以理解我想要的日期一样简单(
-我的机器所在的时区为+02: 00)

解决方案

使用 DateTimeOffset.Parse(string).UtcDateTime


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)

解决方案

Use DateTimeOffset.Parse(string).UtcDateTime.

这篇关于如何将字符串转换为像UTC这样的DateTime就这么简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆