Date Calculation In Batch Files XP/2000

Home -> Windows -> Batch File Programming

26059 views

From the computer of: dtech10 (1 recipe)
Created: Nov 21, 2005


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

Hi
This will calculate the date in +- days time.
There one for dd/mm/yyyy and one for mm/dd/yyyy date formats.
Usage: ThisBatchFile.bat dd/mm/yyyy +-No. Days
ie ThisBatchFile.bat 20/11/2005 -21
The Date in 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

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  Supressing Prompts In Batch Files While Deleting All Files
  Supress Responses From Commands In Batch Files
  Batch Files : Get users default printer via registry (WinXP)
  XP Batch Files: Copy images from camera to pc and organize by date
  Using variables in Windows batch files
  XP batch files: Create variable for current user account and find current user
  Batch File Converting of Video Files: SageTV example
  Create Batch File to Start or End Window Services
  XCOPY command : Using the /EXCLUDE flag
  Windows XP/Vista: Use a Batch File for Quickly Restarting explorer.exe

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.