covert to system date format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buterfly0707
    New Member
    • Nov 2009
    • 30

    covert to system date format

    hi ...

    assume computer system date format is mm/dd/yyyy
    but the format which SQL is getting dd/mm/yyyy. and from the c# code by sql query i have to convert the date to sql datetime format to insert the date to database table . but when i do this im getting an error as
    "invalid date format". and if i get the date as mm/dd/yyyy den also from query i cannot insert it to database table.

    what i want t do here is.. i want to insert date do the database table in correct format. how can i do this??

    i hope you can understand what im saying here. sorry if its not clear.
  • buterfly0707
    New Member
    • Nov 2009
    • 30

    #2
    converting the date..

    hi..

    i want to convert date to sql date format for insert to the database table. i hav edone it like this.

    Code:
       //converting date
        public DateTime Get_Date(string G_date)
            {
                string New_date = "";
                try
                {
                    string[] Date = G_date.Split('/');
                    string oldDate = "";
                    string month = "";
                    if (Date[0].Length == 1)
                    {
                        oldDate = "0" + Date[0];
                    }
                    else
                    {
                        oldDate = Date[0];
                    }
                    if (Date[1].Length == 1)
                    {
                        month = "0" + Date[1];
                    }
                    else
                    {
                        month = Date[1];
                    }
    
                    New_date = oldDate + "/" + month + "/" + Date[2];
                    Convert.ToDateTime(New_date);
                }
                catch { }
            
                return Convert.ToDateTime(New_date);
            }
    and in button click event code:
    Code:
    DateTime rdate = Get_Date(dateTimePicker1.Value.ToShortDateString());
         
    try
    {              
        String S = "insert table1 values('"+ rdate.ToString().Trim() + "') ";
    SqlCommand cmd = new SqlCommand(S);
                        SqlConnection con = new SqlConnection(ConnectionString);
                        SqlDataAdapter da = new SqlDataAdapter(S, con);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
    }
    catch{}
    then when i run the this its giving an error:

    "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
    The statement has been terminated."

    what i have to do to correct this??

    Comment

    • ThatThatGuy
      Recognized Expert Contributor
      • Jul 2009
      • 453

      #3
      Why are you writing such a bulky code for just converting a DateTime....

      you dont need to do so....

      use DateTime.Parse( ) to parse your string date to DateTime object...

      like this...

      DateTime.Parse( "2002-12-21'); //yyyy-mm-dd
      DateTime.Parse( "21-Jan-2002"); //dd-MMM-yyyy
      DateTime.Parse( "12-21-2002"); //dd-mm-yyyy


      will work with '/' also...

      And By Default Sql Server accepts date in mm-dd-yyyy format or yyyy-mm-dd

      ,but not like you're trying .... i.e. dd-mm-yyyy 21-12-2002
      so pass data like 21-Dec-2002

      Comment

      • sanjib65
        New Member
        • Nov 2009
        • 102

        #4
        Actually in SQL Database there is a DateTime data type. If you choose that, no problem should have been arised.

        Comment

        Working...