Page 1 of 1

How to get last day of each month

Posted: 30 Oct 2007 06:36
by supervans
Hi, How can I output last day of each month in dos prompt
example:
20070131
20070228
20070331
20070430

Thanks for your help. :)

Posted: 30 Oct 2007 16:42
by jeb
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