Question

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?

Was it helpful?

Solution 3

The issue is that Visual Basic Script is not case sensitive and my variable month was colliding with the built in Month.

OTHER TIPS

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)

As given here and here

Syntax:

Month(Now)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top