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

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

问题描述

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

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

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



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



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



我收到错误:




字符串未被识别为有效日期时间。



解决方案

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

  DateTime.ParseExact(2013年4月30日23时,MM / DD / YYYY HH:MM,CultureInfo.InvariantCulture)

在这里看到更多的信息:



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


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"

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.

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

I am getting error:

String was not recognized as a valid DateTime.

解决方案

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:

Custom Date and Time Format Strings

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

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