Results 1 to 5 of 5

Thread: How can I manipulate time

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    246

    How can I manipulate time

    How can I manipulate the time so that I can use it for conditional statements?

    For example the Time Function and the TimeSerial Function?

    For example, the program will prompt a message box if the system time is greater than or equal to 8:00 AM and less than or equal to 12:00 PM

    If Time >= TimeSerial(8, 0, 0) Then
    MsgBox "Off-Peak"
    ElseIf Time <= TimeSerial(12, 0, 0) Then
    MsgBox "Off-Peak"
    End If

    using this code I think is wrong.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: How can I manipulate time

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        MsgBox CDbl(Time)
        MsgBox "Earlier than 6:00? " & (Time < 0.25)
        MsgBox "Earlier than 12:00? " & (Time < 0.5)
        MsgBox "Earlier than 18:00? " & (Time < 0.75)
    End Sub
    Time is stored as a fraction. A day changes when a Date datatype's value increases by one. Thus 0.5 = middle of the day.
    Last edited by Merri; Sep 29th, 2008 at 09:54 AM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How can I manipulate time

    Your use of TimeSerial is fine, however your use of If/ElseIf is not quite right - as there will always be an "Off-Peak" message shown.

    The first If checks that the time is 8am or later (ie: 8am to 11:59pm), if it is "Off Peak" is shown.

    The ElseIf will only be reached if the time is less than 8am, and checks if the time is 12pm or earlier (which it must be, as it is less than 8am), so "Off Peak" is shown.


    What you need to do is check two things with the same If statement, which you can do using an And, eg:
    Code:
    If Time >= TimeSerial(8, 0, 0) And Time <= TimeSerial(12, 0, 0) Then 
      MsgBox "Off-Peak"
    End If
    While the method merri posted is valid, it is harder to read for most times, and still requires correct use of If statements.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: How can I manipulate time

    A bit offtopic, but I just have to say it: I find it far easier to understand than the AM/PM system

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How can I manipulate time

    That's fair enough, but I find it easier to read/understand code like this:
    Code:
    If Time >= TimeSerial(8, 0, 0) ...
    (which is basically 24 hour clock)
    ..rather than this:
    Code:
    If Time >= 0.333333333 ...

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