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. General Programming
  3. C#
  4. DateTime

DateTime

Scheduled Pinned Locked Moved C#
question
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.
  • B Offline
    B Offline
    Bahadir Cambel
    wrote on last edited by
    #1

    How could I change the format of the DateTime ? The local Date is in the "dd-mm-yyyy" but SqlDbType is in "mm-dd-yyyy" format . Thanks in advance.

    Q L P 3 Replies Last reply
    0
    • B Bahadir Cambel

      How could I change the format of the DateTime ? The local Date is in the "dd-mm-yyyy" but SqlDbType is in "mm-dd-yyyy" format . Thanks in advance.

      Q Offline
      Q Offline
      quilkin
      wrote on last edited by
      #2

      We use time.ToString("G", DateTimeFormatInfo.InvariantInfo); and you should also consider using time.ToUniversalTime(); before storing times in the database, to avoid problems with daylight saving.

      1 Reply Last reply
      0
      • B Bahadir Cambel

        How could I change the format of the DateTime ? The local Date is in the "dd-mm-yyyy" but SqlDbType is in "mm-dd-yyyy" format . Thanks in advance.

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        If you want to store a date in SQL server, use parametized queeries, and that'll solve the problem, in any format SQL Server uses.

        DateTime myDateTime = .....
        string sql = "UPDATE People SET BirthDate = @Date WHERE ID_person = 1";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@Date", myDateTime);
        cmd.ExecuteNonQuery();
        

        That will save the correct date, no matter what format is the local computer, and/or SQL Server's computer using. Alternatively, there's a way to change the date format for SQL Server, but I don't remember the exact syntax. Using parametized queries, I don't need it. Also, parametized queries help avoid SQL injection attacks, so I'll recommend them for every circumstance. -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        1 Reply Last reply
        0
        • B Bahadir Cambel

          How could I change the format of the DateTime ? The local Date is in the "dd-mm-yyyy" but SqlDbType is in "mm-dd-yyyy" format . Thanks in advance.

          P Offline
          P Offline
          pubududilena
          wrote on last edited by
          #4

          hi, easy way is give dateTime as string Format.. ex:- DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") Now No need to worry when local time and sqldbtype is different. regards, pubudu.

          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