0

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

1
  • 5
    OMG, you should really reconsider this whole method, make it simple, stop putting everything on the same line, and please do read the DateTime.ParseExact documentation, oh and for love of all things beautiful in this world stop abusing null coalescing operators Commented Jun 1, 2018 at 7:19

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This what I needed, I just started writing code in c#, guess I have to learn a lot. Thank you Plexis:)
Glad it helped! Welcome.. And yes, it is a long journey :)
0

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

7 Comments

It works for one format but I receive dates comes in different format. so need to use array, when I use iarray this happens.
How many formats do you want to support? Probably mention them in your question.
I am not sure what would be thrown at me as input...I want to handle all date time formats..is that possible?
Umm, think of “02-04-2012”, would you consider this 2nd April 2012 or 4th Feb 2012?
By default I would consider it as MM-dd-yyyy..? But I get your point that is y=why I put formats in array....not sure if its right way...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.