Solved

Manipulate dates in DOS Bat file

Posted on 2013-05-17
5
653 Views
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:

                     "mm/dd/yyyy_mm/dd/yyyy"
0
Comment
Question by:gantone1
[X]
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
  • 3
5 Comments
 
LVL 54

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"?

~bp
0
 
LVL 43

Expert Comment

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

Steve
0
 
LVL 54

Accepted Solution

by:
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
setlocal

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
SETLOCAL
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%)
EXIT /b

: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
SETLOCAL ENABLEDELAYEDEXPANSION
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%
( ENDLOCAL & REM RETURN VALUES
    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%
)
EXIT /b

Open in new window

~bp
0
 

Author Closing Comment

by:gantone1
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.

Thanks!
0
 
LVL 54

Expert Comment

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

~bp
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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