用VBScript比较五个日期
我编写了以下代码来查找五个日期中最早的日期。这和预期的一样,但我很好奇是否有更优雅的方式来比较五次约会。有人有什么想法吗用VBScript比较五个日期,vbscript,Vbscript,我编写了以下代码来查找五个日期中最早的日期。这和预期的一样,但我很好奇是否有更优雅的方式来比较五次约会。有人有什么想法吗 Dim sTemp sTemp = "" If IsDate(dtOne) Then If IsDate(dtTwo) Then If CDate(dtOne) < CDate(dtTwo) Then sTemp = dtOne Else sTemp = dtTwo
Dim sTemp
sTemp = ""
If IsDate(dtOne) Then
If IsDate(dtTwo) Then
If CDate(dtOne) < CDate(dtTwo) Then
sTemp = dtOne
Else
sTemp = dtTwo
End If
Else
sTemp = dtOne
End If
ElseIf IsDate(dtTwo) Then
sTemp = dtTwo
End If
If IsDate(dtThree) Then
If IsDate(sTemp) Then
If CDate(dtThree) < CDate(sTemp) Then
sTemp = dtThree
End If
Else
sTemp = dtThree
End If
End If
If IsDate(dtFour) Then
If IsDate(sTemp) Then
If CDate(dtFour) < CDate(sTemp) Then
sTemp = dtFour
End If
Else
sTemp = dtFour
End If
End If
If IsDate(dtFive) Then
If IsDate(sTemp) Then
If CDate(dtFive) < CDate(sTemp) Then
sTemp = dtFive
End If
Else
sTemp = dtFive
End If
End If
Dim sTemp
sTemp=“”
如果是IsDate(dtOne),则
如果是IsDate(DT2),则
如果CDate(dtOne)
像这样的东西怎么样,它会进行每次比较,如果传递的变量都不是日期,它会将sTemp恢复为“”:
Dim sTemp
sTemp=“”
sTemp=OldestDate(dtOne,dtTwo)
sTemp=最旧日期(dtThree,sTemp)
sTemp=最旧日期(dtfour,sTemp)
sTemp=最旧日期(DT5,sTemp)
函数OldestDate(dtOne,dtTwo)
如果是IsDate(dtOne),则
如果是IsDate(DT2),则
如果CDate(dtOne)
我会使用如下内容:
sTemp = GetOldestOf(sTemp, dtOne) ' this instruction makes "dtOne" the current
' current oldest date, b/c "sTemp" is auto-
' initialized as Empty
sTemp = GetOldestOf(sTemp, dtTwo)
sTemp = GetOldestOf(sTemp, dtThree)
sTemp = GetOldestOf(sTemp, dtFour)
sTemp = GetOldestOf(sTemp, dtFive)
If isEmpty(sTemp) Then
WScript.Echo "No valid date found!"
Else
WScript.Echo "Oldest date is: " & sTemp
End If
' pre-condition: d1 is either Empty or the current oldest date
Function GetOldestOf(ByVal d1, ByVal d2)
GetOldestOf = d1 ' make d1 the default return value
If IsDate(d2) Then ' d2 is a valid date
d2 = CDate(d2)
If IsEmpty(d1) Then ' if d1 is empty, d2 is automatically oldest
GetOldestOf = d2
ElseIf d1 > d2 Then ' otherwise check if d2 is older
GetOldestOf = d2
End If
End If
End Function
GetOldestOf()
返回第二个参数(转换为日期),前提是该参数为有效日期,且第一个参数为空或比第二个参数新。否则,该函数将返回(未修改的)第一个参数,根据定义,该参数要么是空的,要么是当前最早的日期。您要求一种优雅的方法,然后使用ArrayList,填充它并对其排序。在概念验证代码下面,它不会处理任何日期无效的异常:
' Create a new arraylist
Set arrayList = createobject("System.Collections.ArrayList")
' Loop through all dates
For each d in array("23-5-2007", "28-6-2010", "16-9-2001", "32-12-2000")
' See if the date is valid. If true, convert it to a date and add it to the list
If isDate(d) Then arrayList.Add cDate(d)
Next
' Sort the list from oldest to newest date
arrayList.Sort
' Get the first item, it will be the oldest date (16-9-2001)
OldestDate = arrayList.Item(0)
' Create a new arraylist
Set arrayList = createobject("System.Collections.ArrayList")
' Loop through all dates
For each d in array("23-5-2007", "28-6-2010", "16-9-2001", "32-12-2000")
' See if the date is valid. If true, convert it to a date and add it to the list
If isDate(d) Then arrayList.Add cDate(d)
Next
' Sort the list from oldest to newest date
arrayList.Sort
' Get the first item, it will be the oldest date (16-9-2001)
OldestDate = arrayList.Item(0)