Computing.Net > Forums > Programming > Date Routines in Batch Files

Date Routines in Batch Files

Reply to Message Icon

Original Message
Name: dtech10
Date: July 24, 2007 at 14:43:32 Pacific
Subject: Date Routines in Batch Files
OS: Win XP Pro
CPU/Ram: PIII 1Gb/1Gb
Comment:

Hi
These subroutines may be useful for calculating
date issues in Batch File coding.
The sub-rountines use variables a-f so best
to not to use them in your calling code.
Main varables returned are...
Day = Current Day
Month = Current Month
Year = Current Year (4 digits)
JDate = Julian Date
GDate = Georgian Date
WkDay = Sun Mon Tue ect.
LeapYr = 1=Leap Year, 0 Not Leap Year
DayNoYr = Day Number of Day Month Year
YrNoDate = Reverse of DayNoYr
dd = Day
mm = Month
yyyy = 4 digit Year
yy = 2 Digit Year


--------
rem Examples how they may be used
--------
@echo off
setlocal EnableDelayedExpansion
cls

-------------
rem Calculate Julian Date from Todays Date
-------------
call :Today
call :JDate !Day! !Month! !Year!
echo Julian Date of !Day!/!Month!/!Year! = !JDate!


----------
rem Calculate Number Days between Dates
----------
call :Jdate !Day! !Month! !Year!
set Date1=!JDate!
call :JDate 22 7 2008
set /a NoDays=!JDate!-!Date1!
echo Days between Today and 22/07/2008 = !NoDays!


--
rem Date in Number of Days Time
--
call :JDate !Day! !Month! !Year!
set y=60
set /a x=!Jdate!+!y!
call :GDate !x!
echo Date in !y! Days Time = !dd!/!mm!/!yyyy!
echo Date with leading zeros
call :AddLeadZero !dd! !mm!
echo Date in !y! Days Time = !dd!/!mm!/!yyyy!
call :ShortYr !Year!
call :WkDay !x!
echo Date with Short Year and Weekday
echo Date in !y! Days Time = !WkDay! !dd!/!mm!/!yy!


---
rem Calculate Day Number of Year
---
call :DayNoYr !Day! !Month! !Year!
echo Day Number of Todays Date !Day!/!Month!/!Year! = !DayNoYr!


-
rem Convert Day Number of Year
rem back into a Date
-
call :YrNoDate !DayNoYr! !yyyy!
echo Date from Year Number !DayNoYr! = !dd!/!mm!/!yyyy!


rem --------------
rem Leap Year Test
rem --------------
echo Leap Years
for /l %%a in (2000,1,2010) do (
call:LeapYr %%a
if !LeapYr! EQU 1 (
echo %%a = Leap Year
) else (
echo %%a
)
)


---------------
rem Calculate Age of Files
call :Today
call :Jdate !Day! !Month! !Year!
set Today=!JDate!
echo Today=!Today!
for /f "skip=4 tokens=1,4 delims= " %%a in ('dir /a-d ^| find /v "bytes"') do

(
set x=%%a
set dd=!x:~0,2!
set mm=!x:~3,2!
set yyyy=!x:~6,4!
call :RemoveZeros !dd! !mm!
call :JDate !dd! !mm! !yyyy!
set /a Age=!Today!-!JDate!
call :WkDay !JDate!
echo !WkDay! %%a %%b Age=!Age!
pause > nul
)
exit /b
--------------

---------------
rem Subroutines
---
rem Todays Date variables
rem This needs to be modified
rem for your O/S and Date format
---
rem Params : None
rem Returns: !Day!=Day,!Month!=Month,!Year!=Year
-------------
:Today
for /f "Tokens=1-3 delims=/-" %%a in ('date /t') do (
set Day=%%a
set Month=%%b
set Year=%%c
)
exit /b


rem -----------
rem Julian Date
----------
rem Pamams : %1=Day,%2=Month,%3=Year
rem Returns: !JDate! Julian Date Number
----------
:JDate %1 %2 %3
set a=%2
set b=%3
if !a! LSS 3 (
set /a a+=12
set /a b-=1
)
set /a a+=1
set /a c=!b!/100
set /a d=2-!c!+!c!/4
set /a JDate=(36525*!b!/100)+(306001*!a!/10000)+%1+1720995+!d!
exit /b


rem --------------
rem Gregorian Date
-----------------
rem Pamams : %1=Julian Date Number
rem Returns: !dd!=Day !mm!=Month !yyyy!=Year
-----------------
:GDate %1
set /a a=(%1*100-186721625)/3652425
set /a b=%1+1+!a!-!a!/4
set /a c=!b!+1524
set /a d=(!c!*100-12210)/36525
set /a e=36525*!d!/100
set /a f=(!c!-!e!)*10000/306001
set /a dd=(c-e)-306001*f/10000
if !f! LSS 14 (
set /a mm=!f!-1
) else (
set /a mm=!f!-13
)
if !mm! LSS 3 (
set /a yyyy=!d!-4715
) else (
set /a yyyy=!d!-4716
)
exit /b


rem -------
rem WeekDay
-----
rem Pamams : %1=Julian Date Number
rem Returns: !WkDay!=Day of Week
-----
:WkDay %1
set Data=SunMonTueWedThuFriSat
set /a a=(%1+1)%%7
set /a b=(!a!*3)
set WkDay=!Data:~%b%,3!
exit /b


rem -----------------
rem Add Leading Zeros
---
rem Pamams : %1=Day %2=Month
rem Returns: !dd! and !mm!
with leading zero if value
less than 10.
----
:AddLeadZero %1 %2
if %1 LSS 10 set dd=0%1
if %2 LSS 10 set mm=0%2
exit /b

rem --------------------
rem Remove Leading Zeros
rem --------------------
rem Params : %1=Day,%2=Month
:RemoveZeros %1 %2
set a=%1
set b=%2
if %1 LSS 10 set dd=!a:~1,1!
if %2 LSS 10 set mm=!b:~1,1!
exit /b


rem ----------
rem Short Year
------
rem Pamams : %1=Year !yyyy!
rem Returns:!yy! Two digits Year
------
:ShortYr %1
set a=%1
set yy=!a:~2,2!
exit /b


rem --------------
rem Leap Year Test
----------
rem Pamams : %1=Year
rem Returns: !LeapYr!=1 if Leap Year
and 0 if not
----------
:LeapYr %1
set /a a=%1%%4
set /a b=%1%%100
set /a c=%1%%400
if !a! EQU 0 (
set LeapYr=1
) else (
set LeapYr=0
)
if !b! EQU 0 (
if !c! EQU 0 (
set LeapYr=1
) else (
set LeapYr=0
)
)
exit /b


rem ------------------
rem Day Number of Year
------------
rem Pamams : %1=Day,%2=Month,%3=Year
rem Returns: !DayNoYr! Day Number of Year
------------
:DayNoYr %1 %2 %3
call :LeapYr %3
set /a a=(275*%2/9)-((%2+9)/12)+%1-30
if !a! GTR 59 (
if !LeapYr! EQU 0 (
set /a a-=1
)
)
set DayNoYr=!a!
exit /b


---
rem Date from Day Number of Year
---------------
rem Pamams : %1=Day Number of Year, %2==Year
rem Returns: !dd!=Day !mm!=Month
---------------
:YrNoDate %1 %2
call :LeapYr %2
if !LeapYr! EQU 1 (
set a=1523
) else (
set a=1889
)
set /a b=((%1+!a!)*100-12210)/36525
set /a c=(%1+!a!)-(36525*!b!/100)
set /a d=(!c!*10000/306001)
if !d! GTR 13 (
set /a mm=!d!-13
) else (
set /a mm=!d!-1
)
set /a dd=!c!-(306001*!d!/10000)
exit /b


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: July 24, 2007 at 15:35:09 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

After looking at that, the only thing that comes to mind is, "Any word yet on when Microsoft's going to retire CMD and replace it with PowerShell?"

I'm just saying it'd be nice to get a shell that stores dates as, well, dates.


Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: July 24, 2007 at 16:33:40 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

In theory, it will be the default shell in the next version of Windows, but is available now as a download for XP and up.

http://www.microsoft.com/windowsser...

I say "In theory" because it was supposed to be included in W2k3 but it had too many bugs.


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: July 24, 2007 at 17:15:49 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

...And it was to be the default command shell for Vista...

Actually, I've been messing with PS, and it's kinda fun. (Think: All of the clutter of a batch file, plus more power than VBScript.) The biggest problem for scripting, as I've seen it from my limited exposure, is how it deals with the active directory.


Report Offensive Follow Up For Removal

Response Number 4
Name: FishMonger
Date: July 24, 2007 at 18:55:23 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

I find it interesting that it's taken MS 20+ years to design a shell that mimics the Unix shell (including making aliases for their VB commands to match the names of the Unix commands), but still can't get it to work properly. From the MS articles that I've read, even the currently available download is in beta. Apparently, MS wants the general public to be their beta testers.


Report Offensive Follow Up For Removal

Response Number 5
Name: Razor2.3
Date: July 24, 2007 at 19:08:26 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

Oh, it doesn't mimic the Unix shell (any of 'em) in the way you're thinking. (Or at least the way I think you're thinking.) PowerShell's basically a wrapper for .NET. The pipes transport .NET objects, and they're converted to text only when the pipe ends.

The biggest complaint I've seen so far is that the .NET framework has its own current directory, and PowerShell has its own current directory, so you must manually sync them in your scripts.


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: July 25, 2007 at 03:40:11 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

Hi dtech10,

Interesting stuff. And I'll take your word for it that it works. But only with one date layout. Why did you not use the BIOS as in your post of a few months ago?

As for m$ writing a shell: good luck on that one, girls.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 7
Name: dtech10
Date: July 25, 2007 at 12:48:33 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

Hi Mechanic

I must admit it never entered by mind.
I only wrote the routines as a programming execise and then thought they may be of interest to someone.

Hi Fishmonger.
I don't think Microsoft cares either way about any command line utilities, it just wants use all to use their pretty pictures.
They still have Com files which go back to the old CPM days.
I might try this PoweShell but I bet it won't hold a candle to Unix ShellScript.




Report Offensive Follow Up For Removal

Response Number 8
Name: Razor2.3
Date: July 25, 2007 at 15:31:55 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

I might try this PoweShell but I bet it won't hold a candle to Unix ShellScript.
Great. Subscribe to MS' PowerShell newsgroup (or at least find an archive of it). There's so many quirks about PS, and they've already figured them out.


Report Offensive Follow Up For Removal

Response Number 9
Name: rbarton1
Date: August 16, 2007 at 19:37:23 Pacific
Subject: Date Routines in Batch Files
Reply: (edit)

I needed to print a form from the day before and wanted to check for Leap Years. The routine below was compiled from several different sources and lets the user check any year for Leap Year. I only wanted the results being applied to the Month of March, but this could be adapted to any month.
@ECHO OFF
:TOP
SET /P yyyy=Enter a year
SET mm=03
SET dd=01
SET lp=1
SET nlp=0
SET /a leapYr=%yyyy%%%4
IF %leapYr%==0 (SET /a nlp=%yyyy%%%100)
IF %nlp%==0 (SET /a lp=%yyyy%%%400) ELSE (SET lp=0)
IF %lp%==0 (SET lp=29) ELSE (SET lp=28)
IF %mm%==03 (
IF %dd%==01 (
SET dd=%lp%
) ELSE (
SET /A dd=%dd%-1
)
)
ECHO %dd%
PAUSE
GOTO TOP


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Date Routines in Batch Files

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge