• Status: Solved
• Priority: Medium
• Security: Public
• Views: 523

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
``````

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)
``````

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...
``````

0
New_Alex
• 3
• 2
1 Solution

Retired ProgrammerCommented:
Look up the DateDiff function (f it's available to you).
0

Retired ProgrammerCommented:
``````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
Loop Until Weekday(pdteGetNext) = pintDay
GetNextDate = pdteGetNext

End Function
``````
0

Commented:
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
0

Commented:
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
0

Commented:
The full code should be:

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 = 7 - (GetDateOfEndDay(EndDay) - intWeekdayNow)
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("15:30", "Sunday")
End Sub
0

Author Commented:
Thank you sir.......

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

Thanks
0

Featured Post

• 3
• 2
Tackle projects and never again get stuck behind a technical roadblock.