经典 ASP Vbscript Time() 条件语句未给出正确结果

Classic ASP Vbscript Conditional Statement for Time() not giving correct results

我正在使用 vbscript 编写一个简单的条件语句来检查一个时间是否早于另一个时间。它不起作用或没有任何意义。只使用日期似乎可行,但我需要使用时间...这是行不通的。

dim startDateStartTime, CurrentDateCurrentTime

                startDateStartTime = FormatDateTime("12:00:00 PM")
                    response.Write("The Course Starts at: ["&startDateStartTime&"]<br/>")

                CurrentDateCurrentTime = FormatDateTime("5:00:00 AM")
                    response.Write("The Current Time is: ["&CurrentDateCurrentTime&"]<br/>")


                if CurrentDateCurrentTime < startDateStartTime then

                    response.Write("The current time is less then the course start time. Keep Course open")

                else

                    response.Write("The current time is greater then the course start time. Close Course")

                end if

这条语句的输出是:

课程开始时间:[12:00:00 PM]

当前时间是:[5:00:00 AM]

当前时间晚于课程开始时间。关闭课程

这显然是错误的,因为凌晨 5 点不到中午 12 点。我不明白?

FormatDateTime() returns 一个字符串:

>> WScript.Echo TypeName(FormatDateTime("12:00:00 PM"))
>>
String

所以你的

if CurrentDateCurrentTime < startDateStartTime then

比较以“5”开头的字符串和以“1”开头的字符串。您需要比较(子类型的变量)日期。

要比较 date/time 你应该使用函数 DateDiff

DateDiff(interval, date1, date2, [firstdayofweek], [firstweekofyear])