Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Storing DateTime Format, C# MSSQL DateTime field

Storing DateTime Format, C# MSSQL DateTime field

Scheduled Pinned Locked Moved Database
csharpdatabasesql-serverhelpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AssemblySoft
    wrote on last edited by
    #1

    :doh: I am having trouble storing the date in UK format. I have an aspx page with a DateTime Validator control - works ok. If the Date Text field on the page is entered in US format: mm/dd/yyy my INSERT works fine. However iif the format is UK: dd/mm/yyyy, the INSERT fails. So i got to thinking i should be formatting the date myself after it passes through the Validator. i tried the code below: dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); Is this the correct approach? (full listing shown below) If i do: //values.Add("'" + txtBirth.Text + "'"); and the Date is in US format it works fine, but fails for UK. your help would be greatly appreciated... Carl Full Listing + stuff i've tried: SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [Users] "); sb.Append("(UserID,Login,Password,FirstName,LastName,PhoneNumber,Email,Address,"); sb.Append(" MobileNumber, DateOfBirth,IsAdministrator) "); sb.Append("VALUES ('{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}','{7}','{8}',{9},{10})"); values.Add(Guid.NewGuid().ToString() ); values.Add(txtUser.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); //values.Add(0); if(txtBirth.Text != string.Empty) { //test vars string output="No Output"; DateTime dt; dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); //Test Blocks //block 1 //DateTime dtNow = DateTime.Now; //values.Add("'" + dtNow + "'"); //block 2 //values.Add("'" + txtBirth.Text + "'"); //block 3 //values.Add("'27/11/1999'"); //block 4 //storing the date & time local to the client //DateTime dt; //dt = DateTime.Now; //string output = dt.ToString("G",DateTimeFormatInfo.CurrentInfo); //values.Add("'" + dt + "'"); //block 5 //dt = Convert.ToDateTime(txtBirth.Text); //output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); //values.Add("'" + output + "'"); //block 6 //values.Add("'" + txtBirth.Text + "'"); //test stuff Label1.Text = output; } else values.Add("Null");

    M G 2 Replies Last reply
    0
    • A AssemblySoft

      :doh: I am having trouble storing the date in UK format. I have an aspx page with a DateTime Validator control - works ok. If the Date Text field on the page is entered in US format: mm/dd/yyy my INSERT works fine. However iif the format is UK: dd/mm/yyyy, the INSERT fails. So i got to thinking i should be formatting the date myself after it passes through the Validator. i tried the code below: dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); Is this the correct approach? (full listing shown below) If i do: //values.Add("'" + txtBirth.Text + "'"); and the Date is in US format it works fine, but fails for UK. your help would be greatly appreciated... Carl Full Listing + stuff i've tried: SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [Users] "); sb.Append("(UserID,Login,Password,FirstName,LastName,PhoneNumber,Email,Address,"); sb.Append(" MobileNumber, DateOfBirth,IsAdministrator) "); sb.Append("VALUES ('{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}','{7}','{8}',{9},{10})"); values.Add(Guid.NewGuid().ToString() ); values.Add(txtUser.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); //values.Add(0); if(txtBirth.Text != string.Empty) { //test vars string output="No Output"; DateTime dt; dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); //Test Blocks //block 1 //DateTime dtNow = DateTime.Now; //values.Add("'" + dtNow + "'"); //block 2 //values.Add("'" + txtBirth.Text + "'"); //block 3 //values.Add("'27/11/1999'"); //block 4 //storing the date & time local to the client //DateTime dt; //dt = DateTime.Now; //string output = dt.ToString("G",DateTimeFormatInfo.CurrentInfo); //values.Add("'" + dt + "'"); //block 5 //dt = Convert.ToDateTime(txtBirth.Text); //output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); //values.Add("'" + output + "'"); //block 6 //values.Add("'" + txtBirth.Text + "'"); //test stuff Label1.Text = output; } else values.Add("Null");

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      I suggest either using DateTime.Parse, or using separate entry fields for day, month and year.

      A 1 Reply Last reply
      0
      • M Mike Dimmick

        I suggest either using DateTime.Parse, or using separate entry fields for day, month and year.

        A Offline
        A Offline
        Atilla Ozgur
        wrote on last edited by
        #3

        I suggest use Globalization Classes in .Net using System.Globalization using System.Threading in page load string slang = Request.UserLanguages[0]; Thread.CurrentThread.CurrentCulture = new CultureInfo(slang); With this code according to user preferences you should be able to save datetime properly. If user is american it expect american style, if it is turkish then it expects turkish style. Request.UserLanguages[0] gives you a string in this format en-UK or en-US first two chars is about language second two chars is about culture. You can check user culture and call different function according to that also. Education is no substitute for intelligence. That elusive quality is defined only in part by puzzle-solving ability. It is in the creation of new puzzles reflecting what your senses report that you round out the definition. Frank Herbert

        1 Reply Last reply
        0
        • A AssemblySoft

          :doh: I am having trouble storing the date in UK format. I have an aspx page with a DateTime Validator control - works ok. If the Date Text field on the page is entered in US format: mm/dd/yyy my INSERT works fine. However iif the format is UK: dd/mm/yyyy, the INSERT fails. So i got to thinking i should be formatting the date myself after it passes through the Validator. i tried the code below: dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); Is this the correct approach? (full listing shown below) If i do: //values.Add("'" + txtBirth.Text + "'"); and the Date is in US format it works fine, but fails for UK. your help would be greatly appreciated... Carl Full Listing + stuff i've tried: SqlConnection con; string sql; SqlCommand cmd; StringBuilder sb = new StringBuilder(); ArrayList values = new ArrayList(); sb.Append("INSERT INTO [Users] "); sb.Append("(UserID,Login,Password,FirstName,LastName,PhoneNumber,Email,Address,"); sb.Append(" MobileNumber, DateOfBirth,IsAdministrator) "); sb.Append("VALUES ('{0}', '{1}', '{2}', '{3}','{4}','{5}','{6}','{7}','{8}',{9},{10})"); values.Add(Guid.NewGuid().ToString() ); values.Add(txtUser.Text); values.Add(txtPwd.Text); values.Add(txtFName.Text); values.Add(txtLName.Text); values.Add(txtPhone.Text); values.Add(txtEmail.Text); //values.Add(0); if(txtBirth.Text != string.Empty) { //test vars string output="No Output"; DateTime dt; dt = Convert.ToDateTime(txtBirth.Text); output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); values.Add("'" + output + "'"); //Test Blocks //block 1 //DateTime dtNow = DateTime.Now; //values.Add("'" + dtNow + "'"); //block 2 //values.Add("'" + txtBirth.Text + "'"); //block 3 //values.Add("'27/11/1999'"); //block 4 //storing the date & time local to the client //DateTime dt; //dt = DateTime.Now; //string output = dt.ToString("G",DateTimeFormatInfo.CurrentInfo); //values.Add("'" + dt + "'"); //block 5 //dt = Convert.ToDateTime(txtBirth.Text); //output = dt.ToString("d",DateTimeFormatInfo.InvariantInfo); //values.Add("'" + output + "'"); //block 6 //values.Add("'" + txtBirth.Text + "'"); //test stuff Label1.Text = output; } else values.Add("Null");

          G Offline
          G Offline
          gpa2000
          wrote on last edited by
          #4

          I suggest storing the date as a double. It is the way a computer stores it anyhow. You can't make a mistake when the user changes regional settings e.g. dd/mm/yyyy i.s.o. yyyy-mm-dd Use a double and format it later for display purposes. Grtz, Guus

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups