DateTime.ParseExact字符串格式不断抛出错误 [英] DateTime.ParseExact string format keeps throwing error

查看:437
本文介绍了DateTime.ParseExact字符串格式不断抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串再$ P $日期时间psentation看起来是这样的:

I have a string representation of DateTime that looks like this:

2011-05-25T16:42:17.156Z

我已经试过,没有任何运气如下:

I have tried the following without any luck:

DateTime.ParseExact(formatted, "yyyy-MM-ddThh:mm:ss.fffZ", CultureInfo.CurrentCulture);
DateTime.ParseExact(formatted, "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.CurrentCulture);
DateTime.ParseExact(formatted, CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns(), CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
DateTime.ParseExact(formatted, CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns(), CultureInfo.CurrentCulture, DateTimeStyles.None);

所有这些给了错误:

All of these give the error:

String was not recognized as a valid DateTime.

标准DateTime.Parse似乎工作,虽然性能方面的原因,我们正在探索ParseExact。这似乎是应该pretty的直线前进,但似乎无法得到它的工作。

Standard DateTime.Parse appears to work, though for performance reasons we are exploring ParseExact. This seems like it should be pretty straight forward, but can't seem to get it to work.

推荐答案

使用HH,而不是HH 24小时格式。这应该工作:

Use HH instead of hh for 24 hour format. This should work:

DateTime.ParseExact(formatted, "yyyy-MM-ddTHH:mm:ss.fffZ",
                    CultureInfo.CurrentCulture);

您的确定的要使用当前的文化,而不是固定区域性的关系吗?

Are you sure you want to use the current culture rather than the invariant culture though?

这篇关于DateTime.ParseExact字符串格式不断抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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