Warning: Invalid argument supplied for foreach() in /home/techrecipes/public_html/wp-content/themes/techrecipes/header.php on line 77

Date Calculation in Batch Files XP/2000

The following tutorial describes using a batch file to calculate the date in both English and American formats.


This will calculate the date in +- days time.
One for dd/mm/yyyy and one for mm/dd/yyyy date formats are described.
Usage: ThisBatchFile.bat dd/mm/yyyy +-No. Days
ie ThisBatchFile.bat 20/11/2005 -21
The date 21 days ago

English Date Format dd/mm/yyyy
======================================
@echo off
rem Calculating Dates dd/mm/yyyy Format
setlocal
echo %1 > ~Date.txt
set WDays=WedThuFriSatSunMonTue
for /f “tokens=1-4 delims=/ ” %%a in (~Date.txt) do (
set dd=%%a
set mm=%%b
set yy=%%c
)
if %dd% LSS 10 set dd=%dd:~1%
if %mm% LSS 10 set mm=%mm:~1%

set mo=%mm%
set yr=%yy%
if %mm% LSS 3 (
set /a mo=%mo%+12
set /a yr=%yr%-1
)
set /a mo=%mo%+1
set /a a=%yr%/100
set /a b=2-%a%+%a%/4
set /a jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084

if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%

set /a Pos=(%jd%%%7)*3
set WDay=!WDays:~%Pos%,3!
set /a jd=%jd%+%2
set /a yy=%jd%*100/36525
set /a dd=%jd%-%yy%*36525/100
set /a mm=%dd%*10/306
set /a dd=%dd%-(%mm%*306+5)/10
if %dd%==0 (
set dd=31
set /a mm=%mm%-1
if %mm%==0 set dd=29
)
set /a mm=%mm%+3
if %mm% GTR 12 (
set /a mm=%mm%-12
set /a yy=%yy%+1
)
set /a yy=%yy%+1900
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
echo Today=%WDay% %1
set /a Pos=(%jd%%%7)*3
set WDay=!WDays:~%Pos%,3!
set CDate=%WDay% %dd%/%mm%/%yy%
echo NDate=%CDate%
del ~Date.txt
setlocal
==============================
American Date Format mm/dd/yyyy
==============================
@echo off
rem Calculating Dates mm/dd/yyyy Format
setlocal
echo %1 > ~Date.txt
set WDays=WedThuFriSatSunMonTue
for /f “tokens=1-4 delims=/ ” %%a in (~Date.txt) do (
set mm=%%a
set dd=%%b
set yy=%%c
)
if %dd% LSS 10 set dd=%dd:~1%
if %mm% LSS 10 set mm=%mm:~1%

set mo=%mm%
set yr=%yy%
if %mm% LSS 3 (
set /a mo=%mo%+12
set /a yr=%yr%-1
)
set /a mo=%mo%+1
set /a a=%yr%/100
set /a b=2-%a%+%a%/4
set /a jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084

if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%

set /a Pos=(%jd%%%7)*3
set WDay=!WDays:~%Pos%,3!
set /a jd=%jd%+%2
set /a yy=%jd%*100/36525
set /a dd=%jd%-%yy%*36525/100
set /a mm=%dd%*10/306
set /a dd=%dd%-(%mm%*306+5)/10
if %dd%==0 (
set dd=31
set /a mm=%mm%-1
if %mm%==0 set dd=29
)
set /a mm=%mm%+3
if %mm% GTR 12 (
set /a mm=%mm%-12
set /a yy=%yy%+1
)
set /a yy=%yy%+1900
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
echo Today=%WDay% %1
set /a Pos=(%jd%%%7)*3
set WDay=!WDays:~%Pos%,3!
set CDate=%WDay% %mm%/%dd%/%yy%
echo NDate=%CDate%
del ~Date.txt

The Conversation

Follow the reactions below and share your own thoughts.

  • A09453

    hehe

    • GUEST

      9453 is noob :))