none
How to force datetime format in C#?

    Question

  • How do I convert the value of a textbox into a datetime format with different datetime settings of different machines into a "dd MMM yyyy" format.

    Example 1: If  System Date format is dd/MM/yyyy and user enters date value in a textbox in MM/dd/yyyy and if my code use Convert.ToDateTime(txtDate.text); it gives error "String was not recognized as a valid DateTime".

    Example 2: If System Date format is MM/dd/yyyy and user enters date value in a textbox in dd/MM/yyyy and if mycode use Convert.ToDateTime(txtDate.te); it give error "String was not recognized as a valid DateTime".

    I tried the following code but get errors:

    Code Block

    string st = string.Format("{0:d}",this.txtNewDate.Text);
    DateTime dt = Convert.ToDateTime(st);
    this.txtDate.Text = dt.ToString("dd MMM yyyy");

     

     

    I wanted that no matter what date format is used by System I should be able to convert the user input into a DateTime value with dd MMM yyyy format.

    How can I overcome this error. Need help.

     

    Wednesday, October 24, 2007 9:20 AM

Answers

  • Hi,

     

    Just try this.

     

    CultureInfo obj =new CultureInfo("en-CA");

    DateTime dt = DateTime.Parse("13/12/2007",obj);

     

     

    Regards,

    Manju sandhu

    Wednesday, October 24, 2007 9:54 AM

All replies

  • Hi,

     

    Just try this.

     

    CultureInfo obj =new CultureInfo("en-CA");

    DateTime dt = DateTime.Parse("13/12/2007",obj);

     

     

    Regards,

    Manju sandhu

    Wednesday, October 24, 2007 9:54 AM
  •  

    Dear

     

    I works for dd/MM/yyyy but means 13/12/2007 is converted into 13 Dec 2007 but when I try wih 12/13/2007 it give the same error. Could you give me any other way?

     

    By the way, thanks a lot.

    Wednesday, October 24, 2007 10:52 AM
  • Hi,

     

    Can you tell me what exactly you want.

     

    User can input date in any format and you want to convert it into date time.

     

    or

     

    You want this functionality in two different text boxes.

    In one you insert in the form of dd/MM/yyyy and in another in MM/dd/yyyy

     

    Regards,

    Manju Sandhu

    Wednesday, October 24, 2007 11:18 AM
  • Hi,

     

    I want it the first way i.e., User can input date in any format and I wanted to convert it into date time in dd MMM yyyy (01 Nov 2007) format.

     

    Thanks and regards,

     

    Thursday, November 01, 2007 5:07 AM
  • got the solution ?

     

    - Aananta

    Thursday, November 01, 2007 5:33 AM
  • You want that the user can input date in any format? That does not really make sense...
    For example, if your user input 05/06/2008, how can you define what format he used? How do you know if he meant the 5th june or the 6th May? The only way would be that he tells you what format he uses (not very user friendly...)
    Anyway, you should not give the user the chance to chose its own format.
    The format date should be well defined so that the user knows what he has to put. Better, you should have some dropdownlist or a calendar control to help him populate the value.

    It is the same when you integrate 2 applications. Time and date format might be different between the two, but they are both defined and must be known so that the 2 applications can communicate.

    Best regards,

    Francois Malgreve
    Thursday, November 01, 2007 8:32 AM
  • Thanks !!
    Wednesday, December 08, 2010 4:30 AM