解析ISO 8601字符串日期时间在.NET? [英] Parsing ISO 8601 string to DateTime in .NET?

查看:219
本文介绍了解析ISO 8601字符串日期时间在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,2009-10-08 08:22:02Z,这是在 ISO 8601 格式。

我如何使用日期时间来分析这种格式?

解决方案

 字符串的txt =2009-10-08 08:22:02Z;
日期时间输出= DateTime.ParseExact(TXT,U,System.Globalization.CultureInfo.InvariantCulture);
 

DateTime类支持标准格式字符串ü的这种格式

我觉得对于ISO格式(与T分离器),使用S,而不是U。或使用:

 字符串的txt =2009-10-08 08:22:02Z;
日期时间输出= DateTime.ParseExact(TXT,新的String [] {S型,U},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None);
 

要支持这两种格式。

I have a string, "2009-10-08 08:22:02Z", which is in ISO 8601 format.

How do I use DateTime to parse this format?

解决方案

string txt= "2009-10-08 08:22:02Z";
DateTime output = DateTime.ParseExact(txt, "u", System.Globalization.CultureInfo.InvariantCulture);

The DateTime class supports the standard format string of u for this format

I think for the ISO format (with the T separator), use "s" instead of "u". Or use:

string txt= "2009-10-08 08:22:02Z";
DateTime output = DateTime.ParseExact(txt, new string[] {"s", "u"}, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);

to support both formats.

这篇关于解析ISO 8601字符串日期时间在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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