Results 1 to 4 of 4

Thread: Set time portion of date variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    2

    Set time portion of date variable

    Can someone tell me how I assign the time element only of a date variable?

    The SQL database I have to supply values to has separate fields for the date and time so I want to hold the values in two separate variables prior to calling a stored proc.

    Thanx in advance

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Set time portion of date variable

    Welcome to the forums.

    Try something like:
    VB Code:
    1. Dim strDate As String
    2. Dim strTime As String
    3.  
    4. strDate = Format(Now, "mm/dd/yyyy")
    5. strTime = Format(Now, "hh:mm:ss")

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    2

    Re: Set time portion of date variable

    Hi Hack,

    Thanx for the reply but perhaps I should have been a bit more detailed in my question.

    I have two variables of datatype date as I will be supplying two values to the stored proc. Ultimately the data is displayed on a form which uses two datetime picker controls. One control displays the date, the other the time.

    The value that will provide both the date and time is held in a single cell of a excel spreadsheet, I am not using the current time that Now() would provide.

    Thinking about it I should be able to just set the value of each of the two date variables to the value in the excell cell as that will provide the date and time to both and the controls on the form will display the correct data.

    What I would like to know though is how I would manually set the time portion of a date variable as I can't see examples anywhere I have looked.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Set time portion of date variable

    There are several ways, using Format is one of them

    VB Code:
    1. Dim dteTemp As Date
    2.  
    3. 'set the time only (note date portion is 12/30/1899)
    4. dteTemp = Format(Now, "hh:mm:ss")
    5. dteTemp = TimeSerial(18, 30, 24)
    6.  
    7. 'set the Date & Time
    8. dteTemp = DateSerial(2005, 12, 25) & " " & TimeSerial(10, 30, 33)
    9.  
    10. dteTemp = Date 'time is 00:00:00
    11. dteTemp = DateAdd("h", 10, dteTemp)
    12. dteTemp = DateAdd("n", 4, dteTemp)
    13. dteTemp = DateAdd("s", 7, dteTemp)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width