Convert string into mm/dd/yyyy format

17,475

Solution 1

It fails on the very first term of your format string, which is telling the function to treat the "16" as minutes and to look for hours, minutes, and seconds that don't exist in the input.

You have several different date formats, and so need the ParseExact() overload that accepts several different format strings:

string[] formats= {"dd/MM/yyyy", "dd-MMM-yyyy", "yyyy-MM-dd", 
                   "dd-MM-yyyy", "M/d/yyyy", "dd MMM yyyy"};
string converted = DateTime.ParseExact("16-05-2014", formats, CultureInfo.InvariantCulture, DateTimeStyles.None).ToString("MM/dd/yyyy");

Also remember that lower case "m"s are for minutes. If you want months, you need an upper case "M". Full documentation on format strings is here:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Finally, I suspect you are getting ahead of yourself on formatting the output as a string. Keep these values as DateTime objects for as long as possible, and only format to a string at the last possible moment before showing them to the user. If you really do want a string, at least stick with the ISO 8601 standard format.

Solution 2

Your main problem is that your format string is wrong. A small m is for minute, a big M is for month.
Try to pass all your formats in an array. For example like this

DateTime.ParseExact("16-05-2014",
                    new[] {"dd/MM/yyyy", "dd-MMM-yyyy", "yyyy-MM-dd", 
                        "dd-MM-yyyy", "M/d/yyyy", "dd MMM yyyy"},
                    CultureInfo.InvariantCulture, DateTimeStyles.None);

With this you can parse all your formats at once. For more information about the format settings, see the official docs.

Solution 3

Few things:

Your input date 16/05/2014 doesn't match your format Month/Day/Year - how can there be a 16th month?

Secondly, you're using mm which represents Minutes, not Months. You should use MM.

Finally, your sample string 16-05-2014 doesn't match the format provided, you've used hyphens - instead of forward slashes /

Supply a collection of different formats matching your input:

string[] formats = new [] { "MM/dd/yyyy", "dd-MMM-yyyy", 
                            "yyyy-MM-dd", "dd-MM-yyyy", "dd MMM yyyy" };
DateTime dt = DateTime.ParseExact("05-16-2014", formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
Share:
17,475
user1783170
Author by

user1783170

Updated on June 04, 2022

Comments

  • user1783170
    user1783170 almost 2 years

    I have following strings in different formats:

    16/05/2014
    21-Jun-2014
    2014-05-16
    16-05-2014
    5/19/2014
    14 May 2014
    

    I need to convert all the above strings into mm/dd/yyyy format in c#. I have tried used DateTime.ParseExact as DateTime dt = DateTime.ParseExact("16-05-2014", "mm/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture) in C# but i am getting the exception as "String was not recognized as a valid DateTime".

    I have also tried to use to Convert.ToDateTime() but it is also not working.

    Is there any method or function that we can write/available in C# that would convert the above string formats into a single date format i.e into "mm/dd/yyyy" format ??

    Any help on this would be greatly appreciated.

  • user1783170
    user1783170 over 9 years
    Thanks joel,it really helped.