简体   繁体   English

在 asp.net 核心 3 中,字符串未被识别为有效的 DateTime

[英]String was not recognized as a valid DateTime in asp.net core 3

The date format in an excel upload is returning this error. excel 上传中的日期格式返回此错误。

{"String '6/3/2020 12:00:00 AM' was not recognized as a valid DateTime."}

I was able to fix the issue before here on stackoverflow(Check code) but today while testing the upload again, the problem persists.我之前可以在stackoverflow(检查代码)上解决此问题,但今天再次测试上传时,问题仍然存在。 I have checked almost all the suggestions available on StackOverflow but none seem to be working.我已经检查了 StackOverflow 上几乎所有可用的建议,但似乎没有一个有效。

On the excel sheet I have 6/3/2020 but in the code I got 6/3/2020 12:00:00 AM在 excel 表上,我有6/3/2020 ,但在代码中,我得到6/3/2020 12:00:00 AM

I have been trying to fix this all day我整天都在努力解决这个问题

 for (int i = 2; i <= noOfRow; i++)   //start from the second row
            {
                if (!string.IsNullOrEmpty(workSheet.Cells[i, 3].Text))
                {
                    var date = workSheet.Cells[i, 3].Value.ToString();
                    //will throw exception if the fields in tenor are invalid
                    try
                    {

                        DateTime d = DateTime.ParseExact(date, "M/d/yyyy HH:mm tt", CultureInfo.InvariantCulture);

                    }
                    catch (Exception ex)
                    {
                        validationResult.Message = "Invalid Date.";
                        validationResult.IsValid = false;
                        validationResult.ErrorRowIndex = row;
                        logger.Error(validationResult.Message);
                        break;
                    }
                }
                else
                {
                    validationResult.Message = "Empty Date.";
                    validationResult.IsValid = false;
                    validationResult.ErrorRowIndex = row;
                    logger.Error(validationResult.Message);
                    break;
                }

                ++row;

            }

            return validationResult;
        }

OK, here's your line of code:好的,这是您的代码行:

DateTime d = DateTime.ParseExact(date, "M/d/yyyy HH:mm tt", CultureInfo.InvariantCulture);

And here is the date string you are trying to convert:这是您要转换的日期字符串:

"6/3/2020 12:00:00 AM"

Notice how the date string contains hours, minutes, and seconds, but your format string only has hours and minutes.请注意日期字符串如何包含小时、分钟和秒,但您的格式字符串只有小时和分钟。 DateTime.ParseExact needs you to supply the exact format of the incoming string. DateTime.ParseExact需要您提供传入字符串的确切格式。

Try add seconds to format of date:尝试在日期格式中添加秒数:

Replace代替

DateTime d = DateTime.ParseExact(date, "M/d/yyyy hh:mm tt", CultureInfo.InvariantCulture);

with

    DateTime d = DateTime.ParseExact(date, "M/d/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

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

相关问题 在asp.net中无法将字符串识别为有效的DateTime - String was not recognized as a valid DateTime in asp.net “在Asp.net中,字符串未被识别为有效的DateTime - "String was not recognized as a valid DateTime in Asp.net 字符串未被识别为有效的日期时间,asp.net - String was not recognized as a valid DateTime, asp.net ASP.NET控制器FormatException:字符串未被识别为有效的DateTime - ASP.NET Controller FormatException: String was not recognized as a valid DateTime 在asp.net中,不能将字符串识别为有效的DateTime,托管在godaddy上 - String was not recognized as a valid DateTime in asp.net, hosted on godaddy 发布后出错:字符串未被识别为有效的日期时间 asp.net - Error after publishing : String was not recognized as a valid DateTime asp.net 字符串未被识别为有效日期时间asp.net C# - string was not recognized as a valid datetime asp.net c# Javascript日期到ASP.NET日期:字符串未被识别为有效的DateTime - Javascript date to ASP.NET date: String was not recognized as a valid DateTime 无法将ASP.NET MVC字符串识别为有效的DateTime - ASP.NET MVC String was not recognized as a valid DateTime 字符串不能识别为日期时间格式asp.net? - String is not recognized as datetime format asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM