Help!

or ...
Code:
moonrise = "3/15/2005 22:30:00"
moonset = "3/16/2005 3:30:00"
dDiff = DateDiff("H", moonrise, moonset)

Just convert the input times to the 24 hour clock and it's easy.
 
or ...
Convert everything to seconds and then calculate the time

Code:
Const numSecsInaDay = 86400

'  SUNRISE
'  Grab the hour of the sunrise ...
iHour = DatePart("h", "3/15/2005 10:30:00PM")

'  Convert to seconds ...
lCurrentSecs = (iHour * 60) * 60

'  Secons until midnight ...
lSecsUntilMidnight = numSecsInaDay - lCurrentSecs


'SUNSET
iHour = DatePart("h", "3/16/2005 3:30:00AM")

lSecondsSoFar = (iHour * 60) * 60

lTimeDiffInSeconds = lSecsUntilMidnight + lSecondsSoFar

iTimeDiffInHours = (lTimeDiffInSeconds / 60) / 60
 
the issue is that he can't use any date information, so not sure if that would work. I wrote something, but since smee says his works, I will let John test that first.
 
electron said:
the issue is that he can't use any date information, so not sure if that would work. I wrote something, but since smee says his works, I will let John test that first.
I think mine's the same as harleydude's. It seems to work fine so I don't know what the problem reported with harleydude's is.
 
smee said:
Doesn't something like this work (similar to Harleydude's)?

Code:
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).
This code works for me as well, that's what I was using, I meant Rupp's code, it looked like he was specifying the dates as well.
 
I am wondering if the 12:00 AM being passed in is really being passed in as 12:00 PM. That is the only thing that I can see that would cause the results he is seeing. Hopefully John can post the code and we can have a look see.

Rick
 
I tried all the examples he gave in the first post (using the provided time formatting shown in a few posts below that first post), and that function provided the correct results for me each time. My own function was using datediff the same way, and no problems either.
 
60 * 60 * 1000 - 3600000 miliseconds in one Hour
60 * 60 * 1000 * 24 = 86400000 miliseconds in one Day
86400000 * 364 = 31536000000 miliseconds in one Year

You know you will always have a two day difference because the moon never rises and sets in the same day.

Day(1) MoonRise = 9 AM
Day(2) MoonSet = 12am

Convert Day 1 and 2 into a 24 hour scale

Function CheckForPM(Hour as Int,M as String)
If M = "PM" Then
CheckForPM = Moon + 12
End If
End Function

MoonRise = CheckForPM(9,"AM")
MoonSet = CheckForPM(12,"AM")

Now convert them into a base 10 time by converting them into miliseconds.

Function ConvertToBase10(Hours as int)
ConvertToBase10 = 60 * 60 * 1000 * Hours
End Function

MoonRise = ConvertToBase10(MoonRise)
MoonSet = ConvertToBase10(MoonSet)

You should have a nice number to work with which represents the time of day in miliseconds. Now the easiest way to figure out the difference is to know the day which is available via HS scripting or you can extract the day from the internet if you prefer. But you need to get the day to make this simple.

MoonRise = 32400000
MoonSet = 43200000

You can use VB Scripts built in date function to get your computers system date and time.

CurrentDate = Date(milliseconds) This will give you the date in milli seconds since January 1st 1970 at midnight. So for every 31536000000 milliseconds in your answer is how many years its been since 1970. So this year is 1103760000000(2005) and we are on our 74 day of the year. (74 * 86400000) = Number of milliseconds in this year. Add the to the number of years milliseconds and you get the exact date in milliseconds.

86400000 + 1103760000000 = 1103846400000(Exact Date in milliseconds)

Now add your Time of MoonRise in milliseconds to the date(Which was given to you by the date(milliseconds function) and you will get you time that the moon rose on that specific date. Then when the moon sets the next day or on the same day you can get the date from your system again and do the same math. Now subtract the first number from the second to get the difference. Then devide by the number of milliseconds in a day to get the hour difference.

I'm sure with all this typing i proabably have missed something but what I am trying to say here is that there is a way to do it but you need more information. Either you need to know that the moon will set atleast after 11:59am the same day or you need to know the date.

Either way here is a good site that will teach you all about the date and time functions and what you can do to manipulated the date and time.

Edit sorry this link is for JavaScript but i think vbscript has the same function so i will leave the link here
http://www.devguru.com/Technologies/ecmasc...ckref/date.html
 
I still say the DateDiff example does the job perfectly (unless I am missing something).
 
Squintz said:
I guess im missing the point here. Ill go back and re read everything but i just dont see the need to know when the moon has rose and set. Its not a good indication of the amount of darkness in one night so what would you use this data for.
I assume it's related to this:
Aquarium thread on cocoontech
Aside from the fact that sometimes information is cool in its own right.

electron said:
I still say the DateDiff example does the job perfectly (unless I am missing something).
I think we probably need to have JohnWPB report back. It certainly seems like it should work fine. Maybe there's a problem with the input data, not the function.
 
Well all I can say is thank you for everyone's persistance! I was in a pretty fowl mood last night, not being able to get such a SIMPLE result, after all of the MUCH more complicated scripts/asp pages I have written. Then I go and take it out on the people that are trying to help, please accept my appolagies.

Not one to "throw in the towel" so easily, I will give it one more go:

Here is the script in its current form, with various times for moonrise and current time hardcoded for testing purposes. Just remove the comments character to test each pair of time settings.

* * * I M P O R T A N T * * * *
Also note, that if the moonrise hour is AFTER the current time, it should not
come up with the number of hours up, as the moon has not risen yet. Without a date, the datedif command does not know this, and will just subtract it anyway....

Code:
Dim CurrentTime
Dim Moonrise
Dim iDiff
Dim GetHourDif

sub main()

'CurrentTime = time

' This is for testing remove comments per each section to
' test with various times

'CurrentTime = "11:36am" ' This section should be 14 hours dif. Reports 10
'Moonrise    = "01:00am"

'CurrentTime = "07:50am" ' This section should be 11 hours dif reports 13.
'Moonrise    = "06:00pm"

'CurrentTime = "07:50pm" ' This section should be 10 hours dif reports 14.
'Moonrise    = "05:00am"

'CurrentTime = "12:50pm" ' This section should be 11 hours dif reports 13.
'Moonrise    = "11:00pm"

'CurrentTime = "11:50am" ' This section should be 12 hours dif reports 12 (WORKS!!!).
'Moonrise    = "11:00pm"




   Dim iDiff
   iDiff = DateDiff("H", MoonRise, CurrentTime)
   If CInt(iDiff) < 0 Then
       iDiff = iDiff + 24
   End If
   GetHourDif = iDiff
    hs.setDeviceString "M4",GetHourDif
' Show the total number of hours that the moon has been up.
    msgbox gethourdif
end sub

NOTE:
I notice that everyone is pretty currious why anyone would want this information, and I can understand that for sure! hehehe If you go to My Webpage and go to "My HomeSeer" and then Follow the link that says "Moon Phases" on the HS site. I created a set of graphics to give a visual representation of the location of the moon in relation to the horizon. It was NEVER meant to be this complicated to get the logic behind it working, or I would have never done it. It was just something to work on and another kewl looking display to add to the site.
 
John,

Change the following line:
Code:
  iDiff = DateDiff("H", MoonRise, CurrentTime)

to
Code:
  iDiff = DateDiff("H", CurrentTime, MoonRise)

Try that, it passed with the data you supplied.

Rick
 
electron said:
the issue is that he can't use any date information, so not sure if that would work. I wrote something, but since smee says his works, I will let John test that first.
Then take the date out. It still works

Code:
Const numSecsInaDay = 86400

'  SUNRISE
'  Grab the hour of the sunrise ...
iHour = DatePart("h", "10:30:00PM")

'  Convert to seconds ...
lCurrentSecs = (iHour * 60) * 60

'  Secons until midnight ...
lSecsUntilMidnight = numSecsInaDay - lCurrentSecs


'SUNSET
iHour = DatePart("h", "3:30:00AM")

lSecondsSoFar = (iHour * 60) * 60

lTimeDiffInSeconds = lSecsUntilMidnight + lSecondsSoFar

iTimeDiffInHours = (lTimeDiffInSeconds / 60) / 60
 
Back
Top