-1

Is there a way to get the first and the last day of the previous week into two variables on the windows command line? The date format I'd like to use is YYYY-MM-DD.

Klaus Stadler
  • 395
  • 3
  • 17
  • Perhaps this answer can help? https://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us – Stedy Mar 04 '15 at 17:25

2 Answers2

1

If you can use vbscript, you can try the following:

Create the file vbdate.vbs:

function YMD(d)
    YMD = Year(d) & _
               "-" & Right("00" & Month(d),2) & _
               "-" & Right("00" & Day(d),2) 
end function

set oArgs=WScript.Arguments
' Assuming first day of the week is Monday:
WScript.echo YMD(DateAdd("d", -((Weekday(Now()) + 7 - 2) Mod 7) + oArgs(0), Now()))

If you want Sunday to be first day of the week, replace the last line by:

WScript.echo YMD(DateAdd("d", -((Weekday(Now()) + 7 - 1) Mod 7) + oArgs(0), Now()))

This vbscript takes one argument, beware its sole purpose is to perform the date calculation you asked for:

  • 6 will give you the last day of the current week
  • 0 will give you the first day of the current week
  • -1 will give you the last day of the previous week
  • -7 will give you the first day of the previous week

To put requested values into two variables you can do the following:

@echo off

FOR /F "usebackq tokens=*" %%r in (`CSCRIPT //Nologo "vbdate.vbs" -7`) DO SET RESULT1=%%r
FOR /F "usebackq tokens=*" %%s in (`CSCRIPT //Nologo "vbdate.vbs" -1`) DO SET RESULT2=%%s

REM First day of previous week
ECHO %RESULT1%
REM Last day of previous week
ECHO %RESULT2%
Rubik
  • 1,431
  • 1
  • 18
  • 24
0

I'm not sure how you define your week, but I will assume the week starts with Sunday.

Doing date/time computations in batch is a pain. But the problem can be solved easily if you use my getTimestamp.bat utility - a hybrid JScript/batch script that can be used to perform nearly any date/time computation and formatting task. The utility is pure script that runs natively on any Windows machine from XP onward.

@echo off
setlocal
call getTimestamp /f "{w}" /r dayOfWeekNum
call getTimestamp /od "-%dayOfWeekNum%-7" /f "{yyyy}-{mm}-{dd}" /r beginDate
call getTimestamp /od "-%dayOfWeekNum%-1" /f "{yyyy}-{mm}-{dd}" /r endDate
echo Last week begin date = %beginDate%
echo Last week end date   = %endDate%

EDIT

The above can easily be generalized to support any day as the start of the week. Also, the script could give the wrong result if it is run a split second before midnight on the night it transitions from one week to the next. I solved that below by getting the current date only once, and then performing all computations relative to that date.

The script below assigns Monday as the start of the week.

@echo off
setlocal
set "weekStart=1"  %= 0=Sunday, 1=Monday, 6=Saturday =%
call getTimestamp /f "{ums}" /r today
call getTimestamp /d %today% /f "{w}" /r dayOfWeekNum
set /a "dayOfWeekNum=(7+dayOfWeekNum-weekStart)%%7"
call getTimestamp /d %today% /od "-%dayOfWeekNum%-7" /f "{wkd} {yyyy}-{mm}-{dd}" /r beginDate
call getTimestamp /d %today% /od "-%dayOfWeekNum%-1" /f "{wkd} {yyyy}-{mm}-{dd}" /r endDate
echo Last week begin date = %beginDate%
echo Last week end date   = %endDate%
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Would be a great solution, but unfortunately my administrator has turned off conditional compilation for JScript. – Klaus Stadler Mar 05 '15 at 11:35
  • @KlausStadler - Your admin has disabled JScript, but not VBScript (VBS) ?! Is that possible ? My utility runs JScript via Windows Scripting Host (WSH), not a browser. I know WSH can be disabled, but that would also block VBS. Are you sure your admin hasn't just disabled JScript within the browser? – dbenham Mar 05 '15 at 13:14