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

时间:2022-11-04 01:57:30

im working on a simple Conditional statement in vbscript to check whether a time is earlier than another time. and its not working or making any sense. just using dates seem to work but i need to use time... which isnt working.

我在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

the output for this statement is:

该语句的输出是:

The Course Starts at: [12:00:00 PM]

课程开始于:[12:00:00 PM]

The Current Time is: [5:00:00 AM]

当前时间是:[早上5:00:00]

The current time is greater then the course start time. Close Course

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

which is clearly wrong because 5am is less than 12pm. i dont get it?

这显然是错误的,因为早上5点不到12点。我不明白吗?

2 个解决方案

#1


FormatDateTime() returns a string:

FormatDateTime()返回一个字符串:

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

So your

if CurrentDateCurrentTime < startDateStartTime then

compares a string starting with "5" to a string starting with "1". You need to compare (variables of sub-type) Date(s).

将以“5”开头的字符串与以“1”开头的字符串进行比较。您需要比较(子类型的变量)Date(s)。

#2


For comparing date/time you should use function DateDiff

要比较日期/时间,您应该使用函数DateDiff

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

#1


FormatDateTime() returns a string:

FormatDateTime()返回一个字符串:

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

So your

if CurrentDateCurrentTime < startDateStartTime then

compares a string starting with "5" to a string starting with "1". You need to compare (variables of sub-type) Date(s).

将以“5”开头的字符串与以“1”开头的字符串进行比较。您需要比较(子类型的变量)Date(s)。

#2


For comparing date/time you should use function DateDiff

要比较日期/时间,您应该使用函数DateDiff

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