|
-
Sep 29th, 2008, 09:23 AM
#1
Thread Starter
Addicted Member
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.
-
Sep 29th, 2008, 09:51 AM
#2
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.
-
Sep 29th, 2008, 10:37 AM
#3
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.
-
Sep 29th, 2008, 10:42 AM
#4
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
-
Sep 29th, 2008, 10:45 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|