Convert Date to appropriate format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RP

    #1

    Convert Date to appropriate format

    I am using SQL Server 2005 as backend. I have a Text Box that accepts
    Date in the format dd-MM-yyyy. But when I attempt to insert a record,
    an error is displayed: Cannot convert char to DateTime ....

    Actually, when the text box has entry like 22-10-2005, it considers 22
    as month whereas it is DD. How to convert the Text Box value to an
    acceptable format.

    I tried: DateTime.parse( txtDOB.Text,"dd-MM-yyyy")

    It gives error.

  • Tom Porterfield

    #2
    Re: Convert Date to appropriate format

    RP wrote:
    I am using SQL Server 2005 as backend. I have a Text Box that accepts
    Date in the format dd-MM-yyyy. But when I attempt to insert a record,
    an error is displayed: Cannot convert char to DateTime ....
    >
    Actually, when the text box has entry like 22-10-2005, it considers 22
    as month whereas it is DD. How to convert the Text Box value to an
    acceptable format.
    >
    I tried: DateTime.parse( txtDOB.Text,"dd-MM-yyyy")
    >
    It gives error.
    There is no overload of DateTime.Parse that takes a date and a format.
    Use ParseExact instead. Ex:

    DateTime dt = DateTime.ParseE xact(txtDOB.tex t, "dd-MM-yyyy",
    System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo);
    --
    Tom Porterfield

    Comment

    • RP

      #3
      Re: Convert Date to appropriate format

      Tried as below. But still errors.

      On Aug 16, 11:28 pm, Tom Porterfield <tppor...@mvps. orgwrote:
      There is no overload of DateTime.Parse that takes a date and a format.
      Use ParseExact instead. Ex:
      >
      DateTime dt = DateTime.ParseE xact(txtDOB.tex t, "dd-MM-yyyy",
      System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo);

      Comment

      • joachim@yamagata-europe.com

        #4
        Re: Convert Date to appropriate format

        what's wrong with?

        string inputSplit = txtDOB.Text.Spl it(new char[] { '-' });
        // dd-MM-yyyy to yyyy-MM-dd
        DateTime newDate = new DateTime(
        Convert.ToInt16 (inputSplit[2]),
        Convert.ToInt16 (inputSplit[1]),
        Convert.ToInt16 (inputSplit[0]));

        Regards,
        Joachim


        Comment

        • Rad [Visual C# MVP]

          #5
          Re: Convert Date to appropriate format

          On Thu, 16 Aug 2007 17:40:24 -0000, RP <rpk.general@gm ail.comwrote:
          >I am using SQL Server 2005 as backend. I have a Text Box that accepts
          >Date in the format dd-MM-yyyy. But when I attempt to insert a record,
          >an error is displayed: Cannot convert char to DateTime ....
          >
          >Actually, when the text box has entry like 22-10-2005, it considers 22
          >as month whereas it is DD. How to convert the Text Box value to an
          >acceptable format.
          >
          >I tried: DateTime.parse( txtDOB.Text,"dd-MM-yyyy")
          >
          >It gives error.
          Hi RP,

          You could eliminate 99% of your headaches by using more appropriate
          controls like a datepicker or a calendar. The reason being you do not
          need to write any validation code and your date serialization will be
          independent of gotchas like client pc's regional settings

          --

          Comment

          Working...