如何从 ISO 8601 格式创建 .NET DateTime [英] How to create a .NET DateTime from ISO 8601 format

查看:32
本文介绍了如何从 ISO 8601 格式创建 .NET DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了如何将 DateTime 转换为 ISO 8601 格式,但没有关于如何在 C# 中做相反的事情.

I've found how to turn a DateTime into an ISO 8601 format, but nothing on how to do the reverse in C#.

我有 2010-08-20T15:00:00Z,我想把它变成一个 DateTime 对象.

I have 2010-08-20T15:00:00Z, and I want to turn it into a DateTime object.

我可以自己分离字符串的各个部分,但对于已经是国际标准的东西来说,这似乎需要做很多工作.

I could separate the parts of the string myself, but that seems like a lot of work for something that is already an international standard.

推荐答案

该解决方案利用了 DateTimeStyles 枚举,它也适用于 Z.

This solution makes use of the DateTimeStyles enumeration, and it also works with Z.

DateTime d2 = DateTime.Parse("2010-08-20T15:00:00Z", null, System.Globalization.DateTimeStyles.RoundtripKind);

这完美地打印了解决方案.

This prints the solution perfectly.

这篇关于如何从 ISO 8601 格式创建 .NET DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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