How to get last day of each month

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
supervans
Posts: 1
Joined: 30 Oct 2007 06:25

How to get last day of each month

#1 Post by supervans » 30 Oct 2007 06:36

Hi, How can I output last day of each month in dos prompt
example:
20070131
20070228
20070331
20070430

Thanks for your help. :)

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 30 Oct 2007 16:42

Hi supervans,

try this, it is a little bit simple, but it should work from Year 1604-03 till 2400-01

@echo off
setlocal enableextensions
setlocal enabledelayedexpansion

call :initDateTable
call :getDaysLength 2007 01 res
echo 2007 01 has !res! days
call :getDaysLength 2007 02 res
echo 2007 02 has !res! days

call :getDaysLength 2008 02 res
echo 2008 02 has !res! days
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:initDateTable
set m=0
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) DO (
set /a m += 1
set MonthLength[!m!]=%%a
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:getDaysLength <year> <month> <resultvar>
SETLOCAL
set /a month=10%~2 %% 100
set days=!MonthLength[%month%]!
if "%month%"=="2" (
set /a leap=%1 %% 4
if "!leap!"=="0" (
set days=29
)
)
(
ENDLOCAL
set %~3=%days%
goto :eof
)

ciao
jeb

Post Reply