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. Visual Basic
  4. How to set time AND date using datepicker?

How to set time AND date using datepicker?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
6 Posts 3 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.
  • V Offline
    V Offline
    veljkoz
    wrote on last edited by
    #1

    I need a user to provide the date and time. I'm doing this by using two datepicker controls, one to set date and the other one to set time. But the irritating thing is that I can't combine the two results!? All Date and Time properties of a DateTime class are ReadOnly :wtf: (for whatever the reason), but it would be nice if I could just say: UserDate.Date=pickDate.Date UserDate.Hour=pickTime.Hour UserDate.Minute=pickTime.Minute Can anyone help me how to solve this problem? Thank you, Veljko

    B J 2 Replies Last reply
    0
    • V veljkoz

      I need a user to provide the date and time. I'm doing this by using two datepicker controls, one to set date and the other one to set time. But the irritating thing is that I can't combine the two results!? All Date and Time properties of a DateTime class are ReadOnly :wtf: (for whatever the reason), but it would be nice if I could just say: UserDate.Date=pickDate.Date UserDate.Hour=pickTime.Hour UserDate.Minute=pickTime.Minute Can anyone help me how to solve this problem? Thank you, Veljko

      B Offline
      B Offline
      Briga
      wrote on last edited by
      #2

      I solved it in the past this way: Dim dtD as DateTime = dtpDate.Value Dim dtT as DateTime = dtpTime.Value Dim dtC as DateTime = dtD.AdTicks(dtT.Ticks) I'm not 100% sure about the code since I go by heart, but I'm 100% sure about the method used.

      J 1 Reply Last reply
      0
      • V veljkoz

        I need a user to provide the date and time. I'm doing this by using two datepicker controls, one to set date and the other one to set time. But the irritating thing is that I can't combine the two results!? All Date and Time properties of a DateTime class are ReadOnly :wtf: (for whatever the reason), but it would be nice if I could just say: UserDate.Date=pickDate.Date UserDate.Hour=pickTime.Hour UserDate.Minute=pickTime.Minute Can anyone help me how to solve this problem? Thank you, Veljko

        J Offline
        J Offline
        Joshua Quick
        wrote on last edited by
        #3

        The DateTime type is a Structure. Structures are value types which are typically immutable in the .NET framework, like the String type. The properties of an immutable types cannot be changed. Instead, the immutable object must be overwritten. Why are they like this? Because Structures are passed by value (a copy of the original Structure). So, changing the properties of a returned structure would only change the copy and not the original. Regarding your DateTimePicker handling, you need to overwrite your UserDate variable. Try something like this...

        UserDate = New DateTime(pickDate.Value.Year, pickDate.Value.Month, pickDate.Value.Day, _
        pickTime.Value.Hour, pickTime.Value.Minute, pickTime.Value.Second)

        V 1 Reply Last reply
        0
        • B Briga

          I solved it in the past this way: Dim dtD as DateTime = dtpDate.Value Dim dtT as DateTime = dtpTime.Value Dim dtC as DateTime = dtD.AdTicks(dtT.Ticks) I'm not 100% sure about the code since I go by heart, but I'm 100% sure about the method used.

          J Offline
          J Offline
          Joshua Quick
          wrote on last edited by
          #4

          Briga wrote:

          Dim dtD as DateTime = dtpDate.Value Dim dtT as DateTime = dtpTime.Value Dim dtC as DateTime = dtD.AdTicks(dtT.Ticks)

          This isn't going to work Briga. Even though the user is only entering time into the "dtpTime" DateTimePicker, this object still internally stores a month/day/year and is included in the returned ticks. So, adding the ticks of both the time and date DateTimePickers will push the resulting DateTime beyond the year 4000. Unless of course you are initializing dtpTime to the beginning of time. See my post below for a better solution.

          B 1 Reply Last reply
          0
          • J Joshua Quick

            Briga wrote:

            Dim dtD as DateTime = dtpDate.Value Dim dtT as DateTime = dtpTime.Value Dim dtC as DateTime = dtD.AdTicks(dtT.Ticks)

            This isn't going to work Briga. Even though the user is only entering time into the "dtpTime" DateTimePicker, this object still internally stores a month/day/year and is included in the returned ticks. So, adding the ticks of both the time and date DateTimePickers will push the resulting DateTime beyond the year 4000. Unless of course you are initializing dtpTime to the beginning of time. See my post below for a better solution.

            B Offline
            B Offline
            Briga
            wrote on last edited by
            #5

            Joshua Quick wrote:

            This isn't going to work Briga. Even though the user is only entering time into the "dtpTime" DateTimePicker, this object still internally stores a month/day/year and is included in the returned ticks. So, adding the ticks of both the time and date DateTimePickers will push the resulting DateTime beyond the year 4000. Unless of course you are initializing dtpTime to the beginning of time.

            Yeah I believe you as I said I was going by heart. It did work something similar (the idea is by adding the two components). Anyway your solution seems more elegant and efficient than mine.

            1 Reply Last reply
            0
            • J Joshua Quick

              The DateTime type is a Structure. Structures are value types which are typically immutable in the .NET framework, like the String type. The properties of an immutable types cannot be changed. Instead, the immutable object must be overwritten. Why are they like this? Because Structures are passed by value (a copy of the original Structure). So, changing the properties of a returned structure would only change the copy and not the original. Regarding your DateTimePicker handling, you need to overwrite your UserDate variable. Try something like this...

              UserDate = New DateTime(pickDate.Value.Year, pickDate.Value.Month, pickDate.Value.Day, _
              pickTime.Value.Hour, pickTime.Value.Minute, pickTime.Value.Second)

              V Offline
              V Offline
              veljkoz
              wrote on last edited by
              #6

              Great! Thanks a lot!

              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