Solved

Manipulate dates in DOS Bat file

Posted on 2013-05-17
5
660 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 56

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 56

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 56

Expert Comment

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

~bp
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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