ct_yankee65
ct_yankee65

Reputation: 31

How do I find last Sunday's date and save it in a variable in a Windows batch file

I have a script that needs to connect to an ftp server and download a file that is only created on Sunday and Sunday's yyyy-mm-dd-hh-mm-ss is appended to the file name. I need to find the last Sunday's date (based on today's date, I assume) and convert it to yyyy-mm-dd (I don't care about the time) so I can construct the filename in my ftp script. I have searched a lot of threads on this and other sites, but I'm kind of a novice at batch syntax. I cannot make assumptions about the date format on the machine that will run this script, but it will be in the same timezone as the ftp server and it will be running at least Windows 7. I thought about using the PowerShell solution in HOW to find last SUNDAY DATE through batch but I've read there are issues with PS script portability. Any help is greatly appreciated. Let me know if I need to provide more detail. Thanks!

Upvotes: 2

Views: 4029

Answers (4)

user2379881
user2379881

Reputation: 93

(Get-Date).AddDays(-(get-date).dayofWeek.value__)

Upvotes: 2

mklement0
mklement0

Reputation: 439477

Try the following from a batch file:

for /f "usebackq" %%d in (`powershell -noprofile -command "'{0:yyyy-MM-dd}' -f [DateTime]::Now.AddDays(-1 * [DateTime]::Now.DayOfWeek)"`) do set "lastSunday=%%d"

echo %lastSunday%
:: -> e.g., "2016-02-21", when run on 2016-02-25

To try this directly on the command prompt, replace %%d with %d.

  • The PowerShell expression at the heart of the command,
    [DateTime]::Now.AddDays(-1 * [DateTime]::Now.DayOfWeek),
    which calculates the date of the most recent Sunday, was gratefully borrowed from the answer that you link to in your question.

  • '{0:yyyy-MM-dd}' -f ... applies the desired yyyy-mm-dd formatting to the date.

  • powershell -noprofile command ... invokes the PowerShell expression and outputs its result to stdout.

  • for /f "usebackq" %%d in (`...`) do set lastSunday=%%d captures the output from the PowerShell command and assigns it to batch variable lastSunday.

While invoking PowerShell for just one command from a batch file will be slow, being able to calculate the desired date so conveniently probably outweighs performance concerns.

Upvotes: 0

dbenham
dbenham

Reputation: 130899

Working with date and time using pure batch can be done, but it is not very convenient.

The GetTimestamp.bat utility makes date/time computations and formatting simple within a batch context. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward. The previous link points to the most recent version. The utility was first introduced with a number of examples at http://www.dostips.com/forum/viewtopic.php?t=4847.

Full documentation is available from the command line via getTimestamp /?, or getTimestamp /?? for paged output.

With GetTimestamp, the solution can be as simple as:

@echo off

:: Get the current day of the week, with 0=Sunday, 6=Saturday
:: to be used as an offset from today to get the most recent Sunday
call getTimeStamp -f {w} -r offset

:: Use the offset to get the most recent Sunday in YYYY-MM-DD format
call getTimeStamp -od -%offset% -f {iso-dt} -r lastSunday

:: Show the result
echo lastSunday=%lastSunday%

Upvotes: 0

Wes Larson
Wes Larson

Reputation: 1062

A couple years ago I wrote a batch script to find yesterday's date. I made it able to calculate 'yesterday' based on today's date. It takes into account months ending on the 30th or 31st, and even the next few leap years. The way I wrote it expects the date to be in the format 'Wed 02/24/2016' or 'ddd MM/DD/YYYY', so it may not be useful to you.

As I look at it now, it's probably more complicated than it needs to be and could probably use some cleanup, but it worked for my purposes. You might be able to modify it somehow to make it find last Sunday, instead of yesterday.

set yearCounter=0
set yyyy=%date:~10,4%
set mm=%date:~4,2%
set dd=%date:~7,2%

::use these to override the actual date values for testing
::set yyyy=xxxx
::set mm=xx
::set dd=xx

if %dd%==01 goto LDoM ::Last Day of Month

set DS=%yyyy%%mm%%dd%
set /A yesterday=%DS%-1
goto endyesterday

:LDoM
set /A lastyyyy=%yyyy%-%yearCounter%

if %yesterday:~4,2%==01 set lastmm=12& set lastdd=31& goto LDoY ::Last Day of Year
if %yesterday:~4,2%==02 set lastmm=01& set lastdd=31
if %yesterday:~4,2%==03 set lastmm=02& goto february
if %yesterday:~4,2%==04 set lastmm=03& set lastdd=31
if %yesterday:~4,2%==05 set lastmm=04& set lastdd=30
if %yesterday:~4,2%==06 set lastmm=05& set lastdd=31
if %yesterday:~4,2%==07 set lastmm=06& set lastdd=30
if %yesterday:~4,2%==08 set lastmm=07& set lastdd=31
if %yesterday:~4,2%==09 set lastmm=08& set lastdd=31
if %yesterday:~4,2%==10 set lastmm=09& set lastdd=30
if %yesterday:~4,2%==11 set lastmm=10& set lastdd=31
if %yesterday:~4,2%==12 set lastmm=11& set lastdd=30

set yesterday=%lastyyyy%%lastmm%%lastdd%
goto endYesterday

:february
set leapyear=n
set lastdd=28
if %yesterday:~0,4%==2016 set leapyear=y
if %yesterday:~0,4%==2020 set leapyear=y
if %yesterday:~0,4%==2024 set leapyear=y
if %yesterday:~0,4%==2028 set leapyear=y
if %leapyear%==y set lastdd=29

set yesterday=%lastyyyy%%lastmm%%lastdd%
goto endYesterday

:LDoY
set /A yearCounter=%yearCounter%+1
set /A lastyyyy=%yyyy%-%yearCounter%
set yesterday=%lastyyyy%%lastmm%%lastdd%

:endYesterday
@echo off
echo %yyyy%  %lastyyyy%
echo %mm%    %lastmm%
echo %dd%    %lastdd%
echo.
echo today     = %yyyy%%mm%%dd%
echo yesterday = %yesterday%

Upvotes: 1

Related Questions