简体   繁体   中英

String was not recognized as a valid DateTime while converting Datetime

I have been trying to convert date time format as below, but keep getting this issue. It works fine for first three of the given Example but for the fourth one, it throws an exception. Not sure why such exception is occurring.

Examples :

  • 02-05-2018 12:07:00

  • 02-05-2018 11:56:00

  • 02-05-2018 11:56:00

  • 02-05-2018 14:12:00 (Problem Occurs on this one)

C# Code :

if (item.ReceivedDate != null && item.ReceivedDate != "")
{ 
   string[] formats = { "HH:mm:ss MM-dd-yyyy","hh:mm MM/dd/yyyy","dd-MM-yyyy hh:mm:ss","MM/dd/yyyy", "MM-dd-yyyy", "dd MMM yy", "dd-MMM-yyyy","dd-MM-yyyy", "MM-dd-yyyy", "d-M-yyyy", "d-MMM-yy", "dd-MMM-yy", "d-MMMM-yyyy","M-d-yyyy h:mm:ss tt", "M-d-yyyy h:mm tt",
   "MM-dd-yyyy hh:mm:ss", "M-d-yyyy h:mm:ss","yyyy-MM-dd","dd/MM/YYYY hh:mm","MM/dd/yyyy hh:mm tt","MM/dd/yyyy H:mm","MM/dd/yyyy h:mm tt",
   "M-d-yyyy hh:mm tt", "M-d-yyyy hh tt","MM/dd/yyyy hh:mm:ss","MM/dd/yyyy HH:mm","MMMM dd","yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK","ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’",
   "M-d-yyyy h:mm", "M-d-yyyy h:mm","yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss","yyyy MMMM","dddd, dd MMMM yyyy","dddd, dd MMMM yyyy HH:mm:ss",
   "MM-dd-yyyy hh:mm", "M-dd-yyyy hh:mm",
   "MM-d-yyyy HH:mm:ss.ffffff"};
    string DATE = Convert.ToString(item.ReceivedDate);
    //DATE = DATE.Replace(' ', '-').Replace('/', '-');
    //DATE = DATE.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"));
   string[] NEwDAte = (DATE.ToCharArray()[5] == ' ') ? DATE.Split(' ')[1].Trim().Split('-') : new[] { DATE };
    dr["Receiving Date"] = (DATE.ToCharArray()[5] == ' ') ? (NEwDAte[2] + "-" + NEwDAte[0] + "-" + NEwDAte[1]) : DateTime.ParseExact(DATE, formats, CultureInfo.InvariantCulture, DateTimeStyles.None ).ToString("dd/MM/yyyy");
    //dr["Receiving Date"] = (DATE.ToCharArray()[5] == ' ') ? (NEwDAte[2] + "-" + NEwDAte[0] + "-" + NEwDAte[1]) : Convert.ToDateTime(item.ReceivedDate).ToString("yyyy-MM-dd");
}
else
{ 
    dr["Receiving Date"] = "";
}
    Finaltable.Rows.Add(dr.ItemArray);

Help me resolve this issue. Thanks

This is because the first three are of type 12-Hours, while the last one is of type 24-Hours so when the system try to convert it, it see (14) which is not a hour FOR THE CURRENT FORMAT.

The formats you are using are with (hh) which is for 12-Hours. Use (HH) instead, for 24-Hours.

Why are you putting all the formats? Just use the parsing methods in the DateTime class.

Did you try this?

var d = DateTime.ParseExact("02-05-2018 14:12:00", "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Console.WriteLine(d.ToLongDateString());

Check if this helps.

Have a read of DateTime.ParseExact Method here: https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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