How Can I Get Yesterday’s Date?

How Can I Get Yesterday’s Date?

  • Comments 4
  • Likes
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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Script guy, how do I format yesterday's date to yyyymmdd?

  • This will not give the date in required format, better to go with following batch script and pass the date to the VBS, I have done this and it worked fine without any issues

    Formatted date can be changed in the first line, try various options, you can get more

    set Tdate=%date:~-4,4%%date:~-7,2%%date:~-10,2%

    echo %Tdate%

    @echo off

    for /f "tokens=1" %%i in ('date /t') do set thedate=%%i

    set mm=%thedate:~3,2%

    set dd=%thedate:~0,2%

    set yyyy=%thedate:~6,4%

    ::echo Today : %dd%.%mm%.%yyyy%

    if %dd%==08 (

    set dd=8 ) else (

    if %dd%==09 (

    set dd=9 ) )

    if %mm%==08 (

    set mm=8 ) else (

    if %mm%==09 (

    set mm=9 ) )

    set /A dd=%dd% - 1

    set /A mm=%mm% + 0

    if /I %dd% GTR 0 goto DONE

    set /A mm=%mm% - 1

    if /I %mm% GTR 0 goto ADJUSTDAY

    set /A mm=12

    set /A yyyy=%yyyy% - 1

    :ADJUSTDAY

    if %mm%==1 goto SET31

    if %mm%==2 goto LEAPCHK

    if %mm%==3 goto SET31

    if %mm%==4 goto SET30

    if %mm%==5 goto SET31

    if %mm%==6 goto SET30

    if %mm%==7 goto SET31

    if %mm%==8 goto SET31

    if %mm%==9 goto SET30

    if %mm%==10 goto SET31

    if %mm%==11 goto SET30

    if %mm%==12 goto SET31

    goto ERROR

    :SET31

    set /A dd=31 + %dd%

    goto DONE

    :SET30

    set /A dd=30 + %dd%

    goto DONE

    :LEAPCHK

    set /A tt=%yyyy% %% 4

    if not %tt%==0 goto SET28

    set /A tt=%yyyy% %% 100

    if not %tt%==0 goto SET29

    set /A tt=%yyyy% %% 400

    if %tt%==0 goto SET29

    :SET28

    set /A dd=28 + %dd%

    goto DONE

    :SET29

    set /A dd=29 + %dd%

    :DONE

    if /i %dd% LSS 10 set dd=0%dd%

    if /I %mm% LSS 10 set mm=0%mm%

    ::set YESTERDAY=%dd%.%mm%.%yyyy%

    set YESTERDAY=%yyyy%%mm%%dd%

    echo.

    echo YESTERDAY : %YESTERDAY%

  • Why use a batch file when it is easier tyo do this in VBScript.

    WScript.Echo FormatDate(Date() - 1, vbShortDate )

    Lookup and read about the FormatDate function.

  • PowerShell PS C:\> $today = Get-Date PS C:\> $today.AddDays(-1) Sunday, December 29, 2013 4:24:31 PM