简体   繁体   English

字符串在 c# 错误中未被识别为有效的日期时间

[英]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”这意味着一些事情:

  1. Your day will ALWAYS have 2 digits, meaning a leading 0 (you have 10)你的一天总是有 2 位数字,这意味着前导 0(你有 10)
  2. Your month will ALWAYS have 2 digits, meaning a leading 0 (you have 7)你的月份总是有 2 位数字,意思是前导 0(你有 7)
  3. Your Year will have 4 digits (you have 2019)您的年份将有 4 位数字(您有 2019)
  4. Your separator will be '-' (you have /)您的分隔符将是“-”(您有 /)
  5. You will be using the 12 hour clock, including seconds (you have 12:00:00 AM)您将使用 12 小时制,包括秒(您有 12:00:00 AM)

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”这种格式告诉解析器:

  1. The day will have 2 digits if it needs them, no guaranteed leading 0如果需要,当天将有 2 位数字,不保证前导 0
  2. The month will have 2 digits if it needs them, no guaranteed leading 0如果需要,月份将有 2 位数字,不保证前导 0
  3. The year will have 4 digits年份将有 4 位数字
  4. The separator will be '/'分隔符将是“/”
  5. The time will be using the 12 hour clock, including seconds时间将使用 12 小时制,包括秒

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM