检查变量的最高和最低日期

时间:2014-11-03 11:38:32

标签: vbscript asp-classic

我有六个日期变量。我想查看最高和最低值。

例:

Date1: 2014-06-12 00:00:00.000
Date2: 2014-06-12 00:00:00.000
Date3: 2014-10-20 00:00:00.000
Date4: 2014-10-21 00:00:00.000
Date5: 2014-10-21 00:00:00.000
Date6: 2014-10-24 00:00:00.000

输出应为:

Highest: 2014-10-24 00:00:00.000
 Lowest: 2014-06-12 00:00:00.000

2 个答案:

答案 0 :(得分:3)

Option Explicit

' Prepare data to test
Dim date1, date2, date3, date4, date5, date6

    date1=CDate("2014/06/12")
    date2=CDate("2014/06/12")
    date3=CDate("2014/10/20")
    date4=CDate("2014/10/21")
    date5=CDate("2014/10/21")
    date6=CDate("2014/10/24")

' Initialize array    
Dim aDates
    aDates = Array( date1, date2, date3, date4, date5, date6 )

' Prepare variables to store the required information    
Dim dateMin, dateMax

    dateMin = date1
    dateMax = date1

' Iterate over the array to find the max and min values    
Dim i
    For i=1 to UBound(aDates)
        If aDates(i) < dateMin Then dateMin = aDates(i)
        if aDates(i) > dateMax Then dateMax = aDates(i)
    Next 

' Output Information
    WScript.Echo "Highest: " & CStr( dateMax )
    WScript.Echo " Lowest: " & CStr( dateMin )

答案 1 :(得分:2)

创建一个包含日期的数组并对其进行排序

  Dim DateArray() As DateTime = {"2014-06-12 00:00:00.000", "2014-06-12 00:00:00.000".....your dates here}
  Dim MinDate As DateTime
  Dim MaxDate As DateTime

Array.Sort(DateArray)

      MinDate = DateArray(DateArray.GetLowerBound(0))
      MaxDate = DateArray(DateArray.GetUpperBound(0))