DateTime格式问题:字符串未被识别为有效的DateTime

编程入门 行业动态 更新时间:2024-10-18 21:17:41
本文介绍了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

本文发布于:2023-11-07 19:44:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   未被   格式   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!