Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Manipulate dates in DOS Bat file

Posted on 2013-05-17
Last Modified: 2013-05-18
I need to write a 'bat' file that

- gets the current date
- Calculates the date of the previous Monday
- Calculates the date of a week from the current Sunday

For example the current date is 5/16 so the previous Monday value would be 5/13 and the week from Sunday value would be 5/26.

Then build a string that includes the date range in the format:

Question by:gantone1
  • 3
LVL 53

Expert Comment

by:Bill Prew
ID: 39175095
Does this have to be pure batch, or could a solution that used a small utility (EXE) or a small helper script (VBS) be okay?

Also, if the current day is either Monday, or Sunday could you clarify what day you want calculated in those two "boundary conditions"?

LVL 43

Expert Comment

by:Steve Knight
ID: 39175356
Was going to respond with VBScript suggestion but will leave you with Bill...

LVL 53

Accepted Solution

Bill Prew earned 500 total points
ID: 39175361
Okay, this does what you asked, unless you haven't been clear about all the details.

@echo off

REM Get todays date in julian
call :jdate jToday

REM Calculate what weekday this is (Monday=0)
set /a DOW=(jToday%%7)

REM Calculate prior Monday
set Offset=%DOW%
if %Offset% EQU 0 set Offset=7
set /a jPriorMonday=jToday-%Offset%
call :jdate2date %jPriorMonday% yyyy mm dd
set PriorMonday=%mm%/%dd%/%yyyy%

REM Calculate second next Sunday
set /a Offset=13-%DOW%
set /a jSecondSunday=jToday+%Offset%
call :jdate2date %jSecondSunday% yyyy mm dd
set SecondSunday=%mm%/%dd%/%yyyy%

REM Build and display range
set DateRange=%PriorMonday%_%SecondSunday%
echo %DateRange%

exit /b

REM Subroutines
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
::                -- JD      [out,opt] - julian days
::                -- DateStr [in,opt]  - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20090328 :$categories DateAndTime
:$source http://www.dostips.com
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)

:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I,  MM=100+J,  DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%

Open in new window


Author Closing Comment

ID: 39177449
I have not had a chance to test it but it looks like it will work.  If you don't mind I will contact you if there is a problem.

LVL 53

Expert Comment

by:Bill Prew
ID: 39177799
Sure, let me know how you make out.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question