JSI Tip 8210. How can I determine the number of days in a month?


I have scripted DaysInMonth.bat to return the number of days in a given month.

The syntax for using DaysInMonth.bat is:

call DaysInMonth Month Year Days

Where:

Month is the month number, from 1 - 12.

Year  is the year of Month. If you provide a year less than 100, 2000 will be added to the Year.

Days  is a call directed numeric environment variable that will contain the number of days in the specified Year / Month.
NOTE: For a sample usage, see How do I build a year, month, day folder tree?

DaysInMonth.bat contains:

@echo off
If \{%3\}==\{\} @echo Syntax: Call DaysInMonth Month Year Days&goto :EOF
setlocal
set month=%1
set year=%2
set /a month=100%month%%%100
set /a year=10000%year%%%10000
if %year% LSS 100 set /a year=%year% + 2000
set table=00312831303130313130313031
set /a indx=%month% * 2
call set /a days=%%table:~%indx%,2%%
if %month% NEQ 2 goto :finish
set /a leap=%year%%%4
if %leap% EQU 0 set /a days=29
:finish
endlocal&set /a %3=%days%
NOTE: See the following date related tips:

   0559 - DATE math.
   4835. The Universal Date Parser, modified to also work with Windows XP.
   7045. How do I use modulus to insure that day and month data can be converted to an arithmetic variable?
   7047. How do I convert a date to the day number and day name?
   7081. What is the ISO week number and day for a given date?
   7090. How do I convert an ISO year, week, and day to a calendar date?
   7105. How do I convert a calendar date to a Julian date?
   7106. How do I convert a Julian date to a calendar date?
   7320. How do I convert a date to a Julian day?
   7321. How do I convert a Julian day to a date?
   7322. How do I determine the number of days that have elapsed since the 1st of January 1970 for a given date?   
   7323. How do I convert the number of days that have elapsed since the 1st of January 1970 to a date?
   8181. How can I insure that a year, month, and day contain 4,2, and 2 digits, respectively?



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish