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

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

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

发布评论

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

>www.elefans.com

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