Time Manipulations.

manubabber
manubabber used Ask the Experts™
on
I have to make an application which runs from 21:00 till 06:00 in the morning. Apart from this period the application should sleep.

I get the CurrentTime = Time()
I get the StartTime = 21:00 and the EndTime = 06:00 from the registry.
My problem is that how do I compare the CurrentTime with the StartTime and the EndTime, so that the application does not work and goes to sleep. I do have a sleep function, so thats not a problem. I have a problem doing the time manipulation.

Any help will be highly appreciated.
Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You could use the CDate() function to convert 21:00 and 06:00 to "date" variable types (date var includes the time) then compare the time parts, eg

tmeStartTime = CDate("21:00")
tmeEndTime = CDate("6:00")

if CurrentTime > tmeStartTime OR CurrentTime < tmeEndTime then
  ' Action!
else
  ' sleep
end if


This assumes that it's an overnight run (start = evening, end = morning) - otheriwse, use:

if CurrentTime > tmeStartTime AND CurrentTime < tmeEndTime then


If it might be either, then check that tmeStartTime < tmeEndTime (daytime run) first and use the corresponding if - or combine it into an ugly if statment, ie:

If ((tmeStartTime < tmeEndTime) And (CurrentTime > tmeStartTime AND CurrentTime < tmeEndTime)) or ((tmeStartTime < tmeEndTime) And (CurrentTime > tmeStartTime or CurrentTime < tmeEndTime)) or currenttime = starttime Then




If you compare 21:00 and 06:00 to Now() it will never match as the date returned form now contains todays date and the date held in the variables which hold your two times will hold no date.

For example

now() = 06/12/2002 12:28
Starttime = 00/01/1900  21:00:00
EndTime= 00/01/1900  06:00:00

so you need to add todays date to your times

Commented:
If you compare 21:00 and 06:00 to Now() - maybe that's why the original question compared them to Time() - which does not include today's date.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
Try this

if hour(now) < 6 or hour(now) >=21 then
' run your app
else
sleep
end if
My mistake...

You could use the datediff function.

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

If date1 refers to a later point in time than date2, the DateDiff function returns a negative number

for example

if 0 >= datediff("s",CurrentTime,StartTime) and _
 0 <= datediff("s",CurrentTime,tmeEndTime) then
' run your app
else
sleep
end if

Commented:
Yes, there are numerous date functions - they all depend on the specific details - for example, if the Start/End times will always be on the hour, then you could use the Hour() function - but that wont work if someone uses "21:30" as the start time.

DateDiff() is also handy when looking at a specific 'field', eg datediff("h", ...   but there's not much difference between difference in seconds and comparing the date directly - but always worth knowing about alternatives.  :-)
date diff also takes into account changes in months etc.  I have compared two dates directly and sometime strange things happen, I find that datediff is more reliable

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial