none
Convert different formats of dates in DD/MM/YYYY format in C#

    Question

  • Hello everyone,

    I would like convert any type of date which user can put in Excel in DD/MM/YYYY format. Please suggest how I can achieve this?

     

    Example, Below are the different types of dates Excel can have-

    31/12/2010

    12/31/2010

    1/1/2011

    1/12/2011

    12-12-2010

    12-Dec-10

    12-December-2010

     

    I would like to convert all these dates in DD/MM/YYYY format. Please suggest how I can achieve this.

     

    Thanks,

    Sanket

     

    Thursday, December 23, 2010 2:57 PM

Answers

  • Using TryParseExact, you can pass an array of allowed formats. The first matching one is used.

        string[] dates =
        {
          "31/12/2010",
          "12/31/2010",
          "1/1/2011",
          "1/12/2011",
          "12-12-2010",
          "12-Dec-10",
          "12-December-2010"
        };
        string[] formattedDates = new string[dates.Length];
    
        string[] formats = { "M/d/yyyy", "d/M/yyyy", "M-d-yyyy", "d-M-yyyy", "d-MMM-yy", "d-MMMM-yyyy", };
        for (int i = 0; i < dates.Length; i++)
        {
          DateTime date;
          if (DateTime.TryParseExact(dates[i], formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            formattedDates[i] = date.ToString("dd/MM/yyyy");
        }
    
    • Marked as answer by Paul Zhou Monday, December 27, 2010 9:26 AM
    Thursday, December 23, 2010 4:13 PM

All replies

  •  DateTime date = DateTime.Today;
     string strDate = String.Format("{0:MM/dd/yyyy}", date);
    Be careful, format like you want it, it will not convert from an actual dateTime (dd/MM/yyyy). It has to be the month on 1st place following by days and years on the end (MM/dd/yyyy).
    Thursday, December 23, 2010 3:02 PM
  •  have found another solution for you. This code now convert into yout string format (dd/MM/yyyy):

       DateTime date = DateTime.Today;
       int day = date.Day;
       int month = date.Month;
       int year = date.Year;
       string strDate = day.ToString() + "/" + month.ToString() + "/" + year.ToString();
    

     

    you can replace last row of the code with this:

     string strDate = String.Format("{0}/{1}/{2}", day.ToString(), month.ToString(), year.ToString());

     

    Hope it helps,

    Mitja

    Thursday, December 23, 2010 3:12 PM
  • This is a very problematic issue.

    No solution can guaranty success and I'll explain why:

    Suppose you have this date: 11-12-2010.

    How should the code understand it?

    In the american format it means: Oct-12-2010.

    In the europian: 11-Dec-2010.

    Hoe would a code parse this riddle?

     

    Noam  B.

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Thursday, December 23, 2010 3:58 PM
  • Using TryParseExact, you can pass an array of allowed formats. The first matching one is used.

        string[] dates =
        {
          "31/12/2010",
          "12/31/2010",
          "1/1/2011",
          "1/12/2011",
          "12-12-2010",
          "12-Dec-10",
          "12-December-2010"
        };
        string[] formattedDates = new string[dates.Length];
    
        string[] formats = { "M/d/yyyy", "d/M/yyyy", "M-d-yyyy", "d-M-yyyy", "d-MMM-yy", "d-MMMM-yyyy", };
        for (int i = 0; i < dates.Length; i++)
        {
          DateTime date;
          if (DateTime.TryParseExact(dates[i], formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            formattedDates[i] = date.ToString("dd/MM/yyyy");
        }
    
    • Marked as answer by Paul Zhou Monday, December 27, 2010 9:26 AM
    Thursday, December 23, 2010 4:13 PM
  • Gracias !!
    Tuesday, December 13, 2011 5:44 PM