简体   繁体   English

System.FormatException:字符串未被识别为有效的日期时间 - 尝试转换 MM/DD/YYYY 时

[英]System.FormatException: String was not recognized as a valid DateTime - when trying to convert MM/DD/YYYY

I have following C# method:我有以下 C# 方法:

DateTime ConvertStringToDate(string dateInString)
{
    try {
        //string SSD = dateInString;
        //DateTime date = Convert.ToDateTime(SSD);
        //string strDate = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", date);
        //return Convert.ToDateTime(strDate);

        return DateTime.ParseExact(dateInString, "MM/dd/yyyy", CultureInfo.InvariantCulture);
    }
    catch (Exception) { }
    return DateTime.Today;
} 

The code in comment is another way I tried before.注释中的代码是我之前尝试过的另一种方式。

I am in India and developing an ASP.NET WebForms application for my client in US.我在印度为我在美国的客户开发一个 ASP.NET WebForms 应用程序。 On one of its forms my client will enter the date in TextBox like 6/20/2018 which is MM/dd/yyyy format.在其 forms 之一上,我的客户将在 TextBox 中输入日期,例如 6/20/2018,格式为 MM/dd/yyyy。

But in both the ways I am getting this error: System.FormatException: 'String was not recognized as a valid DateTime.'但是在这两种方式中我都收到了这个错误: System.FormatException: 'String was not recognized as a valid DateTime.'

I tried many solutions on SO but none of them are working.我在 SO 上尝试了很多解决方案,但没有一个有效。

return DateTime.ParseExact(dateInString, "M/d/yyyy", CultureInfo.InvariantCulture);

Check it here 在这里检查

The difference between my answer and Fabulous' one is that I also shortened dd to d so if your user writes 6/6/2018 it will work too 我的回答和Fabulous的区别是我也将dd缩短为d因此,如果您的用户写的是6/6/2018它也可以工作

Your date format is MM/dd/yyyy and your input doesn't match. 您的日期格式为MM / dd / yyyy,并且您输入的内容不匹配。 It is M/dd/yyyy 是M / dd / yyyy

Based on your comment, to solve the 6/1/2018 issue, you'll need to do it as follows M/d/yyyy 根据您的评论,要解决6/1/2018问题,您需要执行以下操作M / d / yyyy

Your format string is missing the AM/PM deisgnator: 您的格式字符串缺少AM / PM设计器:

return DateTime.ParseExact
    (dateInString + " 12:00:00 AM", "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
    // Here ---------------------------------------------^

尝试不与字符串“ AM”串联

I was getting the same exception a couple of days ago when it was 9th of July, I simply appended a 0 with 9 to match the date format. 几天前的7月9日,我遇到了同样的异常,我只是在0后面加上9来匹配日期格式。

Try appending a 0 in your month to match the MM in your date format 尝试在您的月份中附加一个0以匹配您日期格式的MM

Sometimes your system time and date format is not same as test format you can try to change system date and time format有时您的系统时间和日期格式与测试格式不同,您可以尝试更改系统日期和时间格式

If you want to convert this (01-01-2022) type of string into date then try below code.如果您想将这种 (01-01-2022) 类型的字符串转换为日期,请尝试以下代码。

DateTime date = DateTime.ParseExact("01-01-2022", "MM-dd-yyyy", CultureInfo.InvariantCulture);

If you want to convert this (01/01/2022) type of string into date then try below code.如果您想将这种 (01/01/2022) 类型的字符串转换为日期,请尝试以下代码。

DateTime date = DateTime.ParseExact("01-01-2022", "MM/dd/yyyy", CultureInfo.InvariantCulture);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将字符串识别为有效的DateTime(System.FormatException)? - String was not recognized as a valid DateTime(System.FormatException)? 无法将system.formatexception字符串识别为有效的日期时间 - system.formatexception string was not recognized as a valid datetime System.FormatException:字符串未被识别为有效的布尔值 - System.FormatException: String was not recognized as a valid Boolean 试图减去 DateTime “System.FormatException: String '12/9/2019 12:00:00 AM' 未被识别为有效的 DateTime。” - Trying to subtract DateTime "System.FormatException: String '12/9/2019 12:00:00 AM' was not recognized as a valid DateTime." 当系统日期格式为dd / MM / yyyy时,将字符串的日期格式转换为datetime MM / dd / yyyy - Convert date format of string to datetime MM/dd/yyyy when system date formate is dd/MM/yyyy 字符串未被识别为有效的日期时间“格式 dd/MM/yyyy” - String was not recognized as a valid DateTime " format dd/MM/yyyy" c# 中的 DateTime 解析:获取“System.FormatException:”String 未被识别为有效的 DateTime”错误 - DateTime parsing in c#:getting a 'System.FormatException: 'String was not recognized as a valid DateTime' error System.FormatException:字符串未被识别为有效的DateTime(DateTime格式yyyyMMdd:HHmmss) - System.FormatException: String was not recognized as a valid DateTime (DateTime Format yyyyMMdd:HHmmss)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM