New_Alex
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
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...
Thanks for your help guys
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
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
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
' 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you sir.......
That worked Perfectly. Free Beer.....+ Extra Points for you !!!!
Thanks
That worked Perfectly. Free Beer.....+ Extra Points for you !!!!
Thanks