DateTime格式问题字符串未被识别为有效的DateTime [英] Datetime format Issue String was not recognized as a valid DateTime

查看:1021
本文介绍了DateTime格式问题字符串未被识别为有效的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输入的字符串格式化为 MM / DD / YYYY HH:MM:SS在C#的格式

输入的字符串格式 MM / DD / YYYY HH:MM:SS

例如:2013年4月30日23:00

I want to format the input string into MM/dd/yyyy hh:mm:ss format in C#.
The input string is in format MM/dd/yyyy hh:mm:ss
For example :"04/30/2013 23:00"

我试过 Convert.ToDateTime()的功能,但它认为4日期和3月为这不是我想要的。其实一个月04日为03。

I tried Convert.ToDateTime() function, but it considers 4 as date and 3 as month which is not what I want. Actually month is 04 and date is 03.

我试过 DateTime.ParseExact()函数也,但是,得到异常。

I tried DateTime.ParseExact() function also, But getting Exception.

我收到错误

字符串未被识别为有效的DateTime。

String was not recognized as a valid DateTime.

谁能帮帮我吗?

在此先感谢

推荐答案

您的日期时间字符串不包含任何秒。您需要反映在您的格式(删除:SS )。结果
此外,您还需要指定 ^ h 而不是 ^ h 如果您正在使用24小时次:

Your date time string doesn't contains any seconds. You need to reflect that in your format (remove the :ss).
Also, you need to specify H instead of h if you are using 24 hour times:

DateTime.ParseExact("04/30/2013 23:00", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture)

在这里看到更多的信息:

See here for more information:

自定义日期和时间格式字符串

这篇关于DateTime格式问题字符串未被识别为有效的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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