Link to home
Start Free TrialLog in
Avatar of New_Alex
New_AlexFlag for Cyprus

asked on

vbScript, Time difference in seconds until next week.

Function TimeDifference (EndTime, EndDay)

' 
' Msgbox the Time Difference between EndTime of a given week EndDay
' and Local Time Now 
'
' End If

Open in new window



Lets say that today's Local Day is "Tuesday" at 13:30

If I call the Function:

MsgBox TimeDifference ("15:30": "Tuesday")

it will Msgbox "7200" (seconds)

Open in new window



Lets say that today's Local DayTime is "Tuesday" at 13:30

If I call the Function:

MsgBox TimeDifference ("15:30": "Friday")

it will Msgbox X (seconds) from the time now until15:30 on friday...

Open in new window


Thanks for your help guys


Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Look up the DateDiff function (f it's available to you).
MsgBoxt DateDiff("s", Now, GetNextDate(vbTuesday, Now))




Public Function GetNextDate(ByVal pintDay As VbDayOfWeek, ByVal pdteGetNext As Date) As Date
'used to determine the next [passed day of week] from the current date

Do
    pdteGetNext = DateAdd("d", 1, pdteGetNext)
Loop Until Weekday(pdteGetNext) = pintDay
GetNextDate = pdteGetNext

End Function

Open in new window

Function GetDateOfEndDay(EndDay) As Integer
    Select Case UCase(EndDay)
        Case "SUNDAY", "SUN"
            GetDateOfEndDay = 1
        Case "MONDAY", "MON"
            GetDateOfEndDay = 2
        Case "TUESDAY", "TUE"
            GetDateOfEndDay = 3
        Case "WEDNESDAY", "WED"
            GetDateOfEndDay = 4
        Case "THURSDAY", "THU"
            GetDateOfEndDay = 5
        Case "FRIDAY", "FRI"
            GetDateOfEndDay = 6
        Case "SATURDAY", "SAT"
            GetDateOfEndDay = 7
        Case Else
            GetDateOfEndDay = 0
    End Select
End Function

Function TimeDifference(EndTime, EndDay) As Long
    Dim intWeekdayNow As Integer
    Dim intHourNow As Integer
    Dim intMinNow As Integer
    Dim dblTimeNow As Double
   
   
    intWeekdayNow = Weekday(Now)
    intHourNow = Hour(Now)
    intMinNow = Minute(Now)
    dblTimeNow = TimeSerial(intHourNow, intMinNow, 0)
   
    ' Determined seconds from day to day (this will be the same time as Now)
    If GetDateOfEndDay(EndDay) > intWeekdayNow Then
        TimeDifference = GetDateOfEndDay(EndDay) - intWeekdayNow
    Else
        TimeDifference = 6
    End If
    TimeDifference = TimeDifference * 86400
   
    ' now add or subtract seconds depending on which is greater, given amount or Now
   
    If TimeValue(EndTime) > dblTimeNow Then
        TimeDifference = TimeDifference + ((TimeValue(EndTime) - dblTimeNow) * 86400)
    Else
        TimeDifference = TimeDifference - ((dblTimeNow - TimeValue(EndTime)) * 86400)
    End If
   
End Function

Sub test()
    MsgBox TimeDifference("14:45", "Sunday")
End Sub
There's a bug (got a case of the mondays) -
 
    ' Determined seconds from day to day (this will be the same time as Now)
    If GetDateOfEndDay(EndDay) >= intWeekdayNow Then
        TimeDifference = GetDateOfEndDay(EndDay) - intWeekdayNow
    Else
        TimeDifference = 7 - (GetDateOfEndDay(EndDay) - intWeekdayNow)
    End If
    TimeDifference = TimeDifference * 86400
ASKER CERTIFIED SOLUTION
Avatar of Carlos Ramirez
Carlos Ramirez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of New_Alex

ASKER

Thank you sir.......

That worked Perfectly. Free Beer.....+ Extra Points for you !!!!

Thanks