Active Member
I have been going in circles and gettng NOWHERE! I ah HORRIBLE with trying to add or subtract time.

I am working on a lunar calculation script/asp page.

I have 2 times, and I simply want to know the difference in hours between the 2 times. I am not using datedif, as the difference between the 2 times will ALWAYS be less than 24 hours, and that will just complicate things. Also the 2 variables I have to work with are the hour and the ampm state, and do not include the date. So I do NOT want to use date diff.

I simply need to know how many hours are between 2 times, given concideration to AM & PM.

For instance:

8 PM & 9 PM = 1 hour difference
8 PM & 12 AM = 4 hour difference
8 PM & 3 AM = 7 hour difference
11 AM & 1 AM = 14 hour difference

The last time will ALWAYS be after the first time, every time.

if anyone could probide a script snippit simply outputting a single variable holding the difference in time would be fantastic! EA: hourdif = 4

Any help with this is greatly appreceiated!


Senior Member
I might be able to help you but i feel as if i don't have enough information. Where are the time values comming from? Are they in text form such as 8am or in Date Time form as the number of miliseconds since January 1, 1970, 00:00:00 GMT.

If its in text form you are going to need to convert them into integers which can be done fairly simply and if its in Date Time form in miliseconds it would be even easier.

Please describe where the times are comming from. I think you need to add days in somewhere so that you can determine if you are going from 12am one night to 8pm the next which is 20 hours diff or from 8pm one night to 12am the same night which is only 4 hours. If you add days into the picture that problem is easily solved.


Senior Member
After Re-Reading your post i see that there is atleast one variable that is in text form. Is the day available to you in any way?

The idea would we to take what ever the time is i.e. 8 and if its am then do nothing and if its PM then add twelve to convert everything into a 24hour format. The problem of which day it happend still exist tho.


Active Member

I know you mentioned that you did not want to use DateDif, but here is a small snipet of code that I think will work for you.

Function GetHourDif(strTime1, strTime2)
    Dim iDiff
    iDiff = DateDiff("H", strTime1, strTime2)
    If CInt(iDiff) < 0 Then
        iDiff = iDiff + 24
    End If
    GetHourDif = iDiff
End Function

Just put the function into your script and call it and it will return the difference in hours.



Staff member
DateDiff will definitely work, but I did miss that statement that he didn't want to use it as it was early in the morning, my apologies for that :eek:


Active Member

The variables are as follows, as well as a sample of what is contained within them:

MoonriseTime = "08:35 AM"
CurrentTime = "02:43 PM"

I have taken appart the times to seperate the hours, minutes and am/pm into 3 different variables for each. As only hours are needed, and not the minutes, I have used:
IfMoonRiseMiutes > 30 then MoonriseHour = MoonRiseHour + 1
just to round to the closest hour.

HarleyDude: I am not sure the dateDif would even work in this situation as there are NO dates to deal with. The only 2 variables are the Moonrise & set times.

To even make up a date would mean figguring out if the latter(Moonset) time is on the other side of midnight from the earlier(Moonrise) time.

I have continued to go in circles, and was in the hopes that someone could just bail me out with functioning code to handle this.

I simply want to know how many hours the moon has been up.

Here is the code I have so far. The moonrise time has been hardcoded for testing purposes. If you run the code, you will get the current time, as well as the moonrise time in a msgbox. All I need to do is determine how many hours has passed between them. This would be SOOOo easy if it werent for the whole before and after midnight thing!

Dim CurrentTime
Dim mytillmidnight
dim moontillmidnight

Dim Moonrise
Dim MoonRiseHour
Dim MoonAmPm
Dim MoonRiseMinutes
Dim MyHour
Dim MyMinutes
Dim CurrentAmPm

sub main()

CurrentTime = time

' This is a variable pulled from a virtual device normally, and is hardcoded here
' for testing purposes

MoonRise = "08:00am"

MoonRiseHour = left (Moonrise,2)
MoonRiseHour = replace(MoonRiseHour ,":", "")
MoonRiseHour = trim(MoonRiseHour)
if len(MoonRiseHour) = 1 then MoonRiseHour = "0" + MoonRiseHour
MoonAmPm = right (Moonrise,2)
MoonAmPm = lcase(MoonAmPm)

MoonRiseMinutes = right (Moonrise,4)
MoonRiseMinutes = left (MoonRiseMinutes,2)

MyHour = left (CurrentTime,2)
MyHour = replace(MyHour,":", "")
if len(MyHour) = 1 then MyMinutes = mid(CurrentTime,3,2)
if len(MyHour) = 1 then MyHour = "0" + MyHour
if len(MyHour) = 2 then MyMinutes = mid(CurrentTime,3,2)

moonrisehour = replace(moonrisehour,":", "")
 CurrentAmPm= right(CurrentTime,2)
 CurrentAmPm = lcase(CurrentAmPm)

if myminutes > 30 then MyHour = MyHour +1

msgbox "MoonRise =" & MoonRiseHour &  MoonAmPm & "   Current Hour = " & MyHour & CurrentAmPm

end sub


Staff member
what about using a fake date, it doesn't really matter what date you specify if the # of hours is all you want, then datediff() would work.


Active Member
I did not use any dates passed into the function, I passed in 8:00 AM and 9:00 PM and the function returned what I was expecting. Try and see if you get the values you expect.



How about this ? It'll return -5.

sub main()

dim Time1 '
dim Time2 '
dim myTime ' the difference in hours

'---------GET THE TIME INFORMATION-------------------
Time1 = (#march 1, 2005 3:00:00 AM#)
Time2 = (#february 28, 2005 10:00:00 PM#)

myTime = DateDiff("h", Time1, Time2)

msgbox (myTime)

end sub


Active Member
Harleydude, I thought that was it! Was working great UNTIL the clock struck midnight! I ended up with a pumpkin :)

It is showing the time difference between 9:00AM and 12:00AM as 3 hours, when it should be 15 hours :(

It works great if both times are actually the same day, but without a date for a reference, it screws up.

Shawn, As mentioned quite a few times, in a few posts before I do NOT have any dates to work with here. The only thing I have to work with are simply a beginning time, and an ending time.

The logic is just confusing the heck (I wanted to use another word, trust me!) out of me!

If first time is am and second time is am, and if the second hour is smaller than the first hour, then the 2nd hour is probably tommorrow

if first time is am and 2nd time is pm, calculate first time till noon, and 2nd time past noon and add them together.

if first time is pm and the 2nd time is am then again, we have to calculate for midnight..... Argh!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Sorry if I am comming down on anyone, I do not mean to, I am just very frustrated and am calling it quits on this one! I do appreceiate the help, I just thought this was going to be a lot easier than it has turned out to be.



How often do you need to check for this difference in time? Reason I ask is can you just check once an hour and if the time difference is ever over 12 hours throw some sort of flag? (sorry if this isn't much help, just hate seeing someone throw in the towel). :) Just do as you say and think outside the box and you will get it.


Sorry John, someday I'll learn to read threads from the top down, must be my type A personality manifesting again. :)

I understand your agnst. No doubt it can be done -


Active Member

I completely understand your frustrations, I have been there before.

Could you post the code prior to calling the GetHourDif function. I will run it here and see what it turns up.



Senior Member
Doesn't something like this work (similar to Harleydude's)?

function my_time_diff(earlier_time, later_time)
   diff = datediff("h", earlier_time, later_time)
   if diff < 0 then
      diff = diff + 24
   end if
   my_time_diff = diff
end function

I think this should work. I tested it under VBA in Excel (which I've never really used before).