Learn about Windows PowerShell
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
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"
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.
A simplier way to do it:
Output: 3-character weekday (e.g. Mon, Tue) Where date_selected = Month/Day/Year e.g. "9/17/2011"
Thanks for posting this script!
I'm no expert but isnt this out of date?
shouldnt we be doing :
@Mark yes this article is very out of date. It was published in August of 2004. In PowerShell I would use the Get-Date cmdlet, and pick the day of the week off directly. Here is the code: (Get-Date).dayofweek This is it ... no function, no select case needed.
dtDate = Date
strNum = Weekday(dtDate)
strDay = WeekdayName(strNum)
Hi, thx for that!! :) great post guy!!
Greetings! and thx! cu!