Localized/ Dynamic DateTime format string with fixed characters length RRS feed

  • Question

  • User-800406098 posted

    Hello people.

    I hope that someone can help me on this. My problem is the following:

    I have  a multi-language application that has 3 different cultures and almost everything works fine. 

    The problem is that I need is to change the date format string for each culture, but I wish to keep the same lenght to all.

    For example, in en-US culture, the format date string is "M/d/yyyy", but in french is "dd/MM/yyyy".

    What I would like to have, is for example in the en-US culture the format string "MM/dd/yyyy"

    I would like to respect each country's culture, but I would like to add a zero before the day or month if the lenght is equal to 1.

    I know this can be acheived in several ways, but I would like to know if someone has a "clean" one, that could work on the majority of cultures. And if the dashs could be replaced by dashs that would be fantastic :D


    Hope to hear from you soon.

    Kind Regards,

    Thursday, November 12, 2009 4:44 PM

All replies

  • User1577733570 posted

    Hi Chris,

    Please go through below thread-


    Hope it will help you :) 

    Thursday, November 12, 2009 10:29 PM
  • User-800406098 posted

    Hi Leena, thank you for your quick reply.

    The solution that you have supplied is a solution. But I would prefer to avoid that for now.

    What I'm looking for is for something that might be included on the framework already.


    For example, to ensure that the user gets the correct dateTime format string, whether he is using en-US or fr-FR culture, is this:


    IFormatProvider myFormatProvider = Thread.CurrentThread.CurrentCulture;

    txtDate.Text = Convert.ToDateTime(txtDate.Text).ToString(myFormatProvider);


    If the date is, lets say, January, 3rd of 2010, the returned string will be 1/3/2010 (in en-US) and 3/1/2010 (in fr-FR).  The desired value, regardless of which culture is set would be: 01/03/2010 or 03/01/2010. Even cooler would be 01-03-2010 or 03-01-2010 :)

    What I'm almost sure of, is that there is a way, to return those formats in a simple fashion . I just don't know which one that is... But I would love too :)

    Thanks for your help


    Friday, November 13, 2009 12:45 AM