The issue is that Visual Basic Script is not case sensitive and my variable month
was colliding with the built in Month
.
How to get month as integer in Visual Basic Script?
-
11-04-2022 - |
문제
I'm trying to get the current month as a short string in a Visual Basic Script:
Dim month
Dim mth
month = Now.Month ' This doesn't work.
month = Now().Month ' Tried this too.
month = Month(Now) ' Also tried this.
mth = MonthName(month, True) ' (e.g. "Apr" or "Mar")
However, I keep getting runtime errors:
Microsoft VBScript runtime error: Object required: 'Now'
Microsoft VBScript runtime error: Object required: 'Now()'
Microsoft VBScript runtime error: Type mismatch 'Month'
I'm able to use Now
fine as a string:
CStr(Now)
Or as a plain value
Dim val
val = Now
How can I use Now
as an Object for accessing its member functions? Perhaps I am confusing Visual Basic functionality with VB Script?
해결책 3
다른 팁
VBScript Dates are not objects - use the Month(SomeDate) function to get the month number of SomeDate:
>> WScript.Echo Month(Now)
>>
4
To get the month's name (abreviated or full), use:
>> WScript.Echo MonthName(Month(Now()),False)
>> WScript.Echo MonthName(Month(Now()),True)
>>
April
Apr
(stolen from @collapsar, but using the correct (boolean) type of the second parameter).
Official Docs for Month(): here MonthName(): here
[The samples given are 'living' code, if you get errors, your code is to blame]
try this line of code:
MonthName(Month(Now()),1)
a standalone solution:
Dim imonth
Dim mth
imonth = Month(Now()) ' Also tried this.
mth = MonthName(Month(Now()))
WScript.Echo "mth = " & mth
WScript.Echo "mth_short = )" & MonthName(Month(Now()),1)
제휴하지 않습니다 StackOverflow