Hey, Scripting Guy! Given a date, can a script tell me the date that the Friday for that week occurs?

-- DLR

Hey, DLR. Too bad you didn’t ask which dates Saturdays, Sundays, and holidays fall on; coincidentally enough, each of the Scripting Guys have memorized - years in advance - all the days that we aren’t required to come to work. When it comes to work days like Fridays, however, we don’t do quite as well.

But that’s OK: we don’t need to know the dates of all the Fridays because we can get a script to figure that out for us. In fact, here’s a script that - given a date - will report back the date that the Friday of that week falls on:

```dtmDate = #6/23/2005#

intDay = WeekDay(dtmDate)

Wscript.Echo dtmFriday
```

How can such a little script accomplish such an amazing feat? Well, let’s see if we can figure that out. The script begins by assigning the date 6/23/2005 (June 23, 2005) to a variable named dtmDate. Note that the pound signs (#) are optional; we could have surrounded the date with double quote marks instead. However, using pound signs ensures that VBScript interprets the value as a date and not as something else.

Next we use the WeekDay function to determine the day of the week for June 23, 2005 (it’s a Thursday, by the way). WeekDay is going to return one of the following values:

 Constant Value Description vbSunday 1 Sunday vbMonday 2 Monday vbTuesday 3 Tuesday vbWednesday 4 Wednesday vbThursday 5 Thursday vbFriday 6 Friday vbSaturday 7 Saturday

Note. The above table - and the script we just showed you - both assume that Sunday is the first day of the week.

This gives us a value representing the day of the week; in this case we’ll get back a 5, indicating that June 23, 2005 falls on a Thursday. What we need to do now is programmatically determine the number of days between this date and the Friday of that week. That’s something we can do with a single line of code:

```intAdder = 6 - intDay
```

Confused? Don’t be; this actually makes sense. If you refer to the table you’ll see that Friday has a value of 6. If we take the value for Friday (6) and subtract the value for Thursday (5) we get 1. And guess what? If we add 1 day to June 23, 2005 we’ll get the date of the Friday for that week: June 24, 2005. That’s what we do with this line of code:

```dtmFriday = dtmDate + intAdder
```

See how that works? In fact, this simple little equation even works with Saturdays. Suppose we’re checking the date June 25, 2005, which happens to be a Saturday (and has a WeekDay value of 7). Our equation would work out to this:

```intAdder = 6 - 7
```

As you well know, 6 - 7 equals -1. Subtract 1 day from June 25, 2005 and you get June 24, 2005, a Friday. Pretty slick, huh?

Incidentally, there are all sorts of cool things you can do with dates and date arithmetic; for more information, take a look at the VBScript Primer chapter in the Microsoft Windows 2000 Scripting Guide.

As for the Scripting Guys, it’s nice to have a script that can tell you the date that a Friday falls on or the date that a Saturday or Sunday falls on. But we’re not satisfied with that; we plan on writing a script that will turn any given day into a Saturday (just a coincidence that we all get Saturdays off.) If you suddenly stop seeing Hey, Scripting Guy! columns (which aren’t published on Saturdays) you’ll know we succeeded.

Based on the way our initial research is going, however, you can expect to see a new column tomorrow, same as always. (This is turning out to be much harder than we’d hoped.)