How Can I Determine the Day of the Week?

Doctor Scripto

question

Hey, Scripting Guy! I have a script that does certain management tasks based on the day of the week. I know how get the date in a script, but how can I tell whether it’s a Monday or a Tuesday or whatever?

— CT, Tallahassee, FL

answer

Hey, CT. This is actually much easier then it might sound; that’s because VBScript has a built-in function called DatePart that can take any date you give it and tell you everything from the hour to the minute to the day of the week. All you have to do is pass DatePart two items:

The date part you’re looking for.

In this case, we’re looking for the day of the week, so we pass the value “w”. If we were looking for, say, the year, we’d pass the value “y”.

The date in question.

For this script, we simply assign the current date to the variable dtmToday.

Our script ends up looking something like this:

dtmToday = Date()

dtmDayOfWeek = DatePart("w", dtmToday)

Select Case dtmDayOfWeek
    Case 1 Wscript.Echo "Sunday"
    Case 2 Wscript.Echo "Monday"
    Case 3 Wscript.Echo "Tuesday"
    Case 4 Wscript.Echo "Wednesday"
    Case 5 Wscript.Echo "Thursday"
    Case 6 Wscript.Echo "Friday"
    Case 7 Wscript.Echo "Saturday"
End Select

Note the one tricky part: the day of the week comes back as an integer (1 = Sunday, 2 = Monday, etc.). Therefore, for the purposes of this script we used a Select Case statement to convert that integer into the actual day of the week itself.

For more information about the DatePart function, see this portion of the Microsoft Windows 2000 Scripting Guide.

0 comments

Discussion is closed.

Feedback usabilla icon