[英]string not recognized as a valid datetime in c# ERROR
Even after using DateTime.ParseExact
I am getting error as即使在使用
DateTime.ParseExact
我也收到错误
string not recognized as a valid datetime in c#
字符串在 c# 中未被识别为有效的日期时间
Below is my code下面是我的代码
string strIDODDate = DateTime.ParseExact(ObjIp.ID_ODchangeDate, "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
Here is full set of code这是完整的代码集
string strRFCsDate = DateTime.Now.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture);
//string strRFCsDate1 = DateTime.ParseExact("25-09-2019 00:00:00.000", "dd-MM-yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)
// .ToString("dd-MM-yyyy");
if (string.IsNullOrEmpty(ObjIp.ID_ODchangeDate))
{
Tobj.ID_ODchangeDate = strRFCsDate;
}
else
{
//Tobj.ID_ODchangeDate = Convert.ToString(ObjIp.ID_ODchangeDate);
string strIDODDate = DateTime.ParseExact(ObjIp.ID_ODchangeDate, "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
Tobj.ID_ODchangeDate = strIDODDate;
}
update更新
After debugging, I found out that the format which was coming was with exception was below经过调试,我发现即将出现的格式异常如下
10/28/2021 5:34:35 AM : 10/7/2019 12:00:00 AM 10/28/2021 5:34:35 AM : Error : Dumping into Table Process : String was not recognized as a valid DateTime. at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
Unclear from your question, so I'm assuming your incoming date time string is something like 25-09-19 00:00:00.000
您的问题不清楚,所以我假设您的传入日期时间字符串类似于
25-09-19 00:00:00.000
var inDateTime = "25-09-19 00:00:00.000";
string parsedDateTime = DateTime.ParseExact(inDateTime, "dd-MM-yy hh:mm:ss.fff", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
Console.WriteLine(parsedDateTime);
Output输出
25-09-2019
25-09-2019
UPDATE:更新:
Please review: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings请查看: https : //docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
These are the date and time format strings for parsing date times.这些是用于解析日期时间的日期和时间格式字符串。
It's still unclear if your value is October 7 or July 10... Assuming July 10:目前还不清楚您的值是 10 月 7 日还是 7 月 10 日……假设是 7 月 10 日:
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
var inDateTime = "10/7/2019 12:00:00 AM";
string parsedDateTime = DateTime.ParseExact(inDateTime, "dd/M/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
Console.WriteLine(parsedDateTime);
}
}
Output输出
10-07-2019
10-07-2019
See: https://dotnetfiddle.net/2YMb82见: https : //dotnetfiddle.net/2YMb82
Based on your comments and update:根据您的评论和更新:
ObjIp.ID_ODchangeDate was coming in 10/7/2019 12:00:00 AM
ObjIp.ID_ODchangeDate 将于 10/7/2019 12:00:00 AM 到来
You are telling ParseExact
a your format is "dd-MM-yyyy hh:mm:ss tt" this means a few things:你告诉
ParseExact
你的格式是“dd-MM-yyyy hh:mm:ss tt”这意味着一些事情:
The example you gave is not true for 2 and 4 of my list.您给出的示例不适用于我列表中的 2 和 4。 I expect it won't be true for 1 for the first 9 days of any month, because of this you need to specify a format that doesn't include leading 0s and uses the correct date separators.
我希望在任何一个月的前 9 天它都不会为 1,因此您需要指定一种不包含前导 0 并使用正确的日期分隔符的格式。 The format you want is "d/M/yyyy hh:mm:ss tt" this format tells the parser:
你想要的格式是“d/M/yyyy hh:mm:ss tt”这种格式告诉解析器:
That means the code will look like this.这意味着代码将如下所示。 I pulled the formats out into variables for readability, and turned object.property into a variable to make it run simply in fiddle.
我将格式提取到变量中以提高可读性,并将 object.property 转换为变量以使其简单地在小提琴中运行。
string ObjIp_ID_ODchangeDate = "10/7/2019 12:00:00 AM";
string dtFormatIn = "d/M/yyyy hh:mm:ss tt";
string dFormatOut = "dd-MM-yyyy";
string strIDODDate = DateTime.ParseExact(ObjIp_ID_ODchangeDate, dtFormatIn, CultureInfo.InvariantCulture).ToString(dFormatOut);
Console.WriteLine(strIDODDate);
As you can see, this works for your case .如您所见,这适用于您的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.