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. Formatting DateTime

Formatting DateTime

Scheduled Pinned Locked Moved C#
helpdatabasetutorial
5 Posts 5 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.
  • G Offline
    G Offline
    GermanDM
    wrote on last edited by
    #1

    Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh: DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy")); ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanks

    C T F 3 Replies Last reply
    0
    • G GermanDM

      Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh: DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy")); ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanks

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Wouldn't this be easier ? DateTime dt = new DateTime(cpCreateDate.SelectedDate.Year, cpCreateDate.SelectedDate.Month, cpCreateDate.SelectedDate.Day) ? A lot less expensive than string to date conversions.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • G GermanDM

        Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh: DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy")); ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanks

        T Offline
        T Offline
        Tyler45
        wrote on last edited by
        #3

        //you can use substring method like this: cpCreateDate.SelectedDate.ToString("dd/MM/yyyy").Substring(0,10)

        J 1 Reply Last reply
        0
        • T Tyler45

          //you can use substring method like this: cpCreateDate.SelectedDate.ToString("dd/MM/yyyy").Substring(0,10)

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          You can also poke your eyes out with a screwdriver, but it doesnt mean its a good idea. hint: what happens to your code when my date format is long date ie/09 January 2007

          --- How to get answers to your questions[^]

          1 Reply Last reply
          0
          • G GermanDM

            Hi, Ok now this is just weird. i used this line of code everyday and worked fine and suddenly this morning just threw an error in my face :doh: DateTime dt = Convert.ToDateTime(cpCreateDate.SelectedDate.ToString("dd/MM/yyyy")); ok the cpCreateDate control is a datetime picker and the value is for example "09/01/2007 12:00:00 AM" all i want to do is format it to only the date meaning removing the time to save this to a sql database. the problem is that as soon as it converts the string back to a datetime value it keeps on adding the time back again and i get this error when trying to write the value to the database: "The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value. The statement has been terminated." the column in the sql database table is a smalldatetime data type and changing that to normal datetime doesnt resolve this. please help thanks

            F Offline
            F Offline
            Fedor Hajdu
            wrote on last edited by
            #5

            I think you are trying to strip time because you think that small date time is only date field (lots of users think that, i don't know why, i guess there is something like that in Access), but that is not true. This data field just shortens range of valid values while keeping the time. Valid dates for a SMALLDATETIME column can range from January 1, 1900 to June 6, 2079. If I'm wrong, and that is not what error is about than you can simply strip time with: cpCreateDate.SelectedDate.Date;

            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