我用
调用以下函数Call GameTimer(FormatDate(objLiveCommentary("DateFirstStarted"), "WithTime"),
FormatDate(objLiveCommentary("DateSecondStarted"), "WithTime"),
"Soccer")
它打印结果为23,35,64,90。我想取这个结果并将其存储为
CurrentGameTime =
因为我会将CurrentGameTime保存到我的数据库中。我该怎么办?
Function GameTimer (FirstStarted, SecondStarted, GameType)
If GameType = "Soccer" Then
DateFirstStarted = DateDiff("n", FirstStarted, FormatDate(NOW(), "WithTime"))
DateSecondStarted = DateDiff("n", SecondStarted, FormatDate(NOW(), "WithTime"))
If DateFirstStarted <= 45 Then
Response.Write DateFirstStarted
ElseIf DateFirstStarted <= 45 Then
DateFirstStarted
ElseIf DateSecondStarted <= 45 Then
Response.Write DateSecondStarted + 45
ElseIf DateFirstStarted <= 45 Then
DateFirstStarted
Else
Response.Write "90"
End If
End If
End Function
答案 0 :(得分:1)
要返回函数的结果,您需要设置函数返回值:
Function GameTimer(FirstStarted, SecondStarted, GameType)
...
GameTimer = 90
End Function
您的 GameTimer 功能应如下所示:
Function GameTimer(FirstStarted, SecondStarted, GameType)
Dim result
result = 90
If GameType = "Soccer" Then
DateFirstStarted = DateDiff("n", FirstStarted, FormatDateTime(Now(), 0))
DateSecondStarted = DateDiff("n", SecondStarted, FormatDateTime(Now(), 0))
If DateFirstStarted <= 45 Then
result = DateFirstStarted
End If
If DateSecondStarted <= 45 Then
result = DateSecondStarted + 45
End If
End If
GameTimer = result
End Function
这样可行,但它仍然不是干净的代码。
首先,你应该摆脱 GameType ,因为你真正想要的是确定一段时间的长度:
Function GameTimer(FirstStarted, SecondStarted, PeriodInMinutes)
Dim result
result = PeriodInMinutes * 2
DateFirstStarted = DateDiff("n", FirstStarted, FormatDateTime(Now(), 0))
DateSecondStarted = DateDiff("n", SecondStarted, FormatDateTime(Now(), 0))
If DateFirstStarted <= PeriodInMinutes Then
result = DateFirstStarted
End If
If DateSecondStarted <= PeriodInMinutes Then
result = DateSecondStarted + PeriodInMinutes
End If
GameTimer = result
End Function
<强>用法强>
CurrentGameTime = GameTimer(FormatDateTime(objLiveCommentary("DateFirstStarted"), 0),
FormatDateTime(objLiveCommentary("DateSecondStarted"), 0),
45)
下一步是用Array替换FirstStarted和SecondStarted参数,以允许有三分之二和四分之一的游戏。
期间开始时间数组
Function GameTimer(Periods, PeriodInMinutes)
Dim result
Dim currenttime
Dim i
result = 0 '-- preset to zero --'
For i = 1 To (UBound(Periods))
currenttime = DateDiff("n", Periods(i), FormatDateTime(Now(), 0))
If currenttime > 0 Then
If (currenttime <= PeriodInMinutes * i) Then
result = currenttime + (PeriodInMinutes * (i - 1))
Else
result = PeriodInMinutes * i
End If
End If
Next
GameTimer = result
End Function
<强>用法强>
Dim CurrentGameTime
Dim IceHockeyPeriods(3)
IceHockeyPeriods(1) = "2010-04-15 19:30"
IceHockeyPeriods(2) = "2010-04-15 20:00"
IceHockeyPeriods(3) = "2010-04-15 20:30"
CurrentGameTime = GameTimer(IceHockeyPeriods, 20)
修改强>
重构以纠正在返回全部时间段之间的缺陷。