02. marts 2005 - 14:39Der er
11 kommentarer og 1 løsning
Beregning af tid, som falder imellem 2 tidspunkter i en periode
Hej,
Jeg skal buge en ASP funktion, som kan beregne hvor mange minutter der falder inden for et bestemt tidsrum i en periode.
Eksemple: Tidsrum: 15:00 til 18:00 alle søndage Periode: 02/20/2005 16:00 til 02/28/2005 19:00 Funktionen skal returnere hvor mange minutter i perioden der falder inden for tidsrummet. I overstående teksemple vil det være 300 minuter.
Var ikke sikker på hvordan den ville opføre sig, hvis den kun fik time/min tallet. Så jeg ville gør det til en rigtig dato.
Gik desuden ud fra at jqp får de forskellige datoer/tider fra en variabel, også bruger jeg right funktionen til at pille time/min ud, og smide dem i en ny variabel.
Du ser fodbold hver søndag imellem 15:00 og 18:00. Jeg kunne godt tænke mig en funktion, der kan beregne hvor mange minuters fodbold du så i perioderne 2004-01-01 til 2004-12-31 og 2005-02-01 til 2005-02-28.
Fik lavet denne her funktion, som gerne skulle kunne klare det:
Response.Write calcServiceWindow("06-03-2005 16:00:00", "13-03-2005 19:00:00", "15:00:00", "18:00:00", vbSunday) '** Calculate Service Window in minutes (SW) - SW is a specific timespan on a weekday **' Function calcServiceWindow(timestampStart, timestampEnd, SWstart, SWend, SWday) Dim countSWdays, FirstSundayDiff, LastSundayDiff, DaysInPeriod, SWlength DaysInPeriod = DateDiff("d", timestampStart, timestampEnd) SWlength = DateDiff("n", SWstart, SWend) countSWdays = 0
'** Get number of SWday's in period **' If DaysInPeriod => 1 Then countSWdays = Int(DaysInPeriod / 7) '** If =>8, period must contain an extra SWday Or If 1, period end and start on SWday **' If (Weekday(timestampStart) + DaysInPeriod Mod 7) => 8 Or Weekday(timestampStart) = SWday Then countSWdays = countSWdays + 1 End If
'** If first or last date is a SWday **' If DatePart("w", timestampStart, SWday) = 1 Then '** Are period only one day? **' If DateDiff("d", timestampStart, timestampEnd) = 0 And DateDiff("n", FormatDateTime(timestampEnd, 3), SWend) > 0 Then If DateDiff("n", FormatDateTime(timestampStart, 3), SWstart) < 0 Then FirstSundayDiff = DateDiff("n", FormatDateTime(timestampStart, 3), FormatDateTime(timestampEnd, 3)) Else FirstSundayDiff = DateDiff("n", SWstart, FormatDateTime(timestampEnd, 3)) End If Else '** Period is more than one day FirstSundayDiff = DateDiff("n", FormatDateTime(timestampStart, 3), SWstart) If FirstSundayDiff > 0 Then FirstSundayDiff = 0 : countSWdays = countSWdays + 1 '** Full SW If FirstSundayDiff < (SWlength*-1) Then FirstSundayDiff = 0 '** Starts after SW ends If FirstSundayDiff <> 0 Then FirstSundayDiff = SWlength - (FirstSundayDiff*-1) '** Some SW countSWdays = countSWdays -1 End If End If If DatePart("w", timestampEnd, SWday) = 1 And DateDiff("d", timestampStart, timestampEnd) > 0 Then LastSundayDiff = DateDiff("n", SWend, FormatDateTime(timestampEnd, 3)) If LastSundayDiff > 0 Then LastSundayDiff = 0 : countSWdays = countSWdays + 1 '** Full SW If LastSundayDiff < (SWlength*-1) Then LastSundayDiff = 0 '** Ends before SW starts If LastSundayDiff <> 0 Then LastSundayDiff = SWlength - (LastSundayDiff*-1) '** Some SW countSWdays = countSWdays -1 End If calcServiceWindow = (SWlength * countSWdays) + Abs(FirstSundayDiff) + Abs(LastSundayDiff) End Function
'** Get number of SWday's in period **' countSWdays = Int(DaysInPeriod / 7) '** If =>8, period must contain an extra SWday Or If 1, period end and start on SWday **' If (Weekday(timestampStart) + DaysInPeriod Mod 7) => 8 Or Weekday(timestampStart) = SWday Then countSWdays = countSWdays + 1
'** If first or last date is a SWday **' If DatePart("w", timestampStart, SWday) = 1 Then If DateDiff("n", FormatDateTime(timestampStart, 3), SWend) < 0 Then '** X<XXX>O - Period starts after SW ends FirstSundayDiff = 0 ElseIf DateDiff("n", FormatDateTime(timestampEnd, 3), SWstart) > 0 And _ DaysInPeriod = 0 Then '** O<XXX>X - Period ends before SW starts FirstSundayDiff = 0 ElseIf DateDiff("n", FormatDateTime(timestampStart, 3), SWstart) < 0 And _ DateDiff("n", FormatDateTime(timestampEnd, 3), SWend) > 0 And _ DaysInPeriod = 0 Then '** X<XOX>X - Period starts before SW starts and ends before SW ends FirstSundayDiff = DateDiff("n", FormatDateTime(timestampStart, 3), FormatDateTime(timestampEnd, 3)) ElseIf DateDiff("n", FormatDateTime(timestampStart, 3), SWstart) < 0 And _ (DateDiff("n", FormatDateTime(timestampEnd, 3), SWend) < 0 Or _ DaysInPeriod > 0) Then '** X<XOO>O - Period starts after SW starts and ends after SW ends FirstSundayDiff = DateDiff("n", FormatDateTime(timestampStart, 3), SWend) ElseIf DateDiff("n", FormatDateTime(timestampStart, 3), SWstart) > 0 And _ DateDiff("n", FormatDateTime(timestampEnd, 3), SWend) > 0 And _ DaysInPeriod = 0 Then '** O<OOX>X - Period starts before SW starts and ends before SW ends FirstSundayDiff = DateDiff("n", SWstart, FormatDateTime(timestampEnd, 3)) ElseIf DateDiff("n", FormatDateTime(timestampStart, 3), SWstart) > 0 And _ (DateDiff("n", FormatDateTime(timestampEnd, 3), SWend) < 0 Or _ DaysInPeriod > 0) Then '** O<OOO>O - Period starts before SW starts and ends after SW ends FirstSundayDiff = SWlength End If countSWdays = countSWdays -1 End If If DatePart("w", timestampEnd, SWday) = 1 And DateDiff("d", timestampStart, timestampEnd) > 0 Then LastSundayDiff = DateDiff("n", SWend, FormatDateTime(timestampEnd, 3)) If LastSundayDiff > 0 Then LastSundayDiff = 0 : countSWdays = countSWdays + 1 '** Full SW If LastSundayDiff < (SWlength*-1) Then LastSundayDiff = 0 '** Ends before SW starts If LastSundayDiff <> 0 Then LastSundayDiff = SWlength - (LastSundayDiff*-1) '** Some SW countSWdays = countSWdays -1 End If calcServiceWindow = (SWlength * countSWdays) + Abs(FirstSundayDiff) + Abs(LastSundayDiff) End Function
En noget bedre funktion. Løsningen er baseret på en løsning modtaget fra en anden "hjælpe" side:
Response.Write getSWminutes("06/03/2005 17:00", "13/03/2005 17:00", "15:00:00", "18:00:00", vbSunday) Function getSWminutes(sd, ed, WindowStart, WindowEnd, WindowDay) Dim Windows : Windows = DateDiff("ww", sd, ed, WindowDay) Dim Interval : Interval = DateDiff("n", WindowStart, WindowEnd) If WeekDay(sd) = WindowDay Then Windows = Windows + 1 getSWminutes = Windows * Interval - SWBoundary(sd, WindowStart, Interval, WindowDay, False) - SWBoundary(ed, WindowStart, Interval, WindowDay, True) End Function
Function SWBoundary(d, WindowStart, Interval, WindowDay, LastDay) If WeekDay(d) = WindowDay Then SWBoundary = DateDiff("n",FormatDateTime(d,2) & " " & WindowStart,d) If SWBoundary < 0 Then SWBoundary = 0 If SWBoundary > Interval Then SWBoundary = Interval If LastDay Then SWBoundary = Interval - SWBoundary Else SWBoundary = 0 End If End Function
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.