How Can I Get Yesterday’s Date?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m writing a script, and I need to know yesterday’s date. I can use Date() to figure out today’s date, but how do I figure out yesterday’s date?

— SS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SS. You’re right; it’s easy to determine today’s date using a script:

dtmToday = Date()
Wscript.Echo dtmToday

But what about yesterday’s date? Listen, we’ve got good news for you: it’s just as easy to determine yesterday’s date as it is today’s date. That’s because VBScript can do date arithmetic: give it today’s date, and then you can simply subtract 1 day to determine yesterday’s date. In other words:

dtmYesterday = Date() - 1
Wscript.Echo dtmYesterday

Want to know tomorrow’s date? You’re way ahead of us:

dtmTomorrow = Date() + 1
Wscript.Echo dtmTomorrow

Cool, huh? Incidentally, you can do even fancier date arithmetic using VBScript. Want to know the date that’s 37 days from July 30, 2004? You can calculate that using the DateAdd function:

dtmDate = DateAdd("d", 37, "July 30, 2004")
Wscript.Echo dtmDate

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

0 comments

Discussion is closed.

Feedback usabilla icon