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

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


0
New_Alex
Asked:
New_Alex
  • 3
  • 2
1 Solution
 
Martin LissRetired ProgrammerCommented:
Look up the DateDiff function (f it's available to you).
0
 
Martin LissRetired 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
    pdteGetNext = DateAdd("d", 1, pdteGetNext)
Loop Until Weekday(pdteGetNext) = pintDay
GetNextDate = pdteGetNext

End Function

Open in new window

0
 
slycoderCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
slycoderCommented:
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
 
slycoderCommented:
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
 
New_AlexAuthor Commented:
Thank you sir.......

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

Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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