解析简单的DateTime [英] Parse Simple DateTime

查看:202
本文介绍了解析简单的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTime dt = DateTime.ParseExact("1122010", "Mddyyyy", System.Globalization.CultureInfo.CurrentCulture);

抛出此异常:String未被识别为有效的DateTime。

Throwing this exception: String was not recognized as a valid DateTime.

我相信这是本月份缺乏领先的0。我们建议使用格式MMddyyyy,并确保您的输入参数至少包含8个字符。

I'm sure it's the lack of a leading 0 in the month. What's the correct format string?

推荐答案

例如:

DateTime dt = DateTime.ParseExact("1122010".PadLeft(8, '0'), "MMddyyyy", System.Globalization.CultureInfo.CurrentCulture);

如果您使用的数据源中缺少本月的前导0,则会将其添加到

If you are using a data source with the leading 0 missing for the month, this will add it where required.

这篇关于解析简单的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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