Solved

Manipulate dates in DOS Bat file

Posted on 2013-05-17
5
616 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
  • 3
5 Comments
 
LVL 51

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 51

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 51

Expert Comment

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

~bp
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now