### how can i find number of days in a given year and month

by ganesh_saravanan (Initiate)
 on Jul 16, 2001 at 22:25 UTC Need Help??
Contributed by ganesh_saravanan on Jul 16, 2001 at 22:25 UTC
Q&A  > dates and times

 Answer: how can i find number of days in a given year and monthcontributed by Celada The mktime has a nice feature which you can exploit for this purpose: you can supply values that are out of range. What happens if you give as input to mktime the zeroth day of this month? Why, you get the last day of last month, of course! mktime is part of your standard C library, so you should get results consistent with everything else on the system, too (that's why I'm not so fond of some of the solutions proposed so far which reimplement the logic). So the solution is to ask for the zeroth day of the month following the one you are interested in, and see what you get. Also you don't have to worry about what happens in December and you ask for the following month (the 13th month) because mktime deals with that too. ```#!/usr/bin/perl use POSIX; (\$month, \$year) = (12, 2005); \$lastday = POSIX::mktime(0,0,0,0,\$month-1+1,\$year-1900,0,0,-1); @_ = localtime(\$lastday); print \$_[3], "\n"; [download]``` Note: silly -1+1 used to clarify that we are putting the month into the 0 .. 11 range expected by mktime (-1) and asking for the following month (+1). Answer: how can i find number of days in a given year and monthcontributed by brian_d_foy I've grown fond of Date::Simple. ```use Date::Simple qw(days_in_month); my \$days = days_in_month( 2004, 2 ); [download]``` Answer: how can i find number of days in a given year and monthcontributed by grinder ```#! /usr/bin/perl -w use strict; use Date::Calc qw/Days_in_Month/; while( ) { chomp; my( \$year, \$month ) = split; my \$days = eval { Days_in_Month( \$year, \$month) }; \$days = 'who knows?' if \$@; print "\$year/\$month => \$days\n"; } __DATA__ 1996 1 1996 2 1998 2 2000 2 1900 2 1600 2 2001 2 2001 7 2001 12 2001 13 2001 0 [download]``` hint: if you know your data are clean then you can omit the eval and error check. Answer: how can i find number of days in a given year and monthcontributed by tye ```my @monthDays= qw( 31 28 31 30 31 30 31 31 30 31 30 31 ); sub MonthDays { my \$month= shift(@_); my \$year= @_ ? shift(@_) : 1900+(localtime())[5]; if( \$year <= 1752 ) { # Note: Although September 1752 only had 19 days, # they were numbered 1,2,14..30! return 19 if 1752 == \$year && 9 == \$month; return 29 if 2 == \$month && 0 == \$year % 4; } else { return 29 if 2 == \$month and 0 == \$year%4 && 0 == \$year%100 || 0 == \$year%400; } return \$monthDays[\$month-1]; } [download]``` This covers Julian and Gregorian dates and uses Julian month lengths even prior to the Julian calendar. Answer: how can i find number of days in a given year and monthcontributed by davorg You can achive this without using external modules: ```#!/usr/bin/perl -w use strict; use Time::Local; while( ) { chomp; my (\$year, \$month) = split; do { warn "Invalid month: \$month\n"; next } if \$month > 12 or \$month < 1; my \$next_year = (\$month == 12) ? \$year + 1 : \$year; my \$next_month = timelocal(0, 0, 0, 1, \$month % 12, \$next_year); my \$days = (localtime(\$next_month - 86_400))[3]; print "\$year/\$month => \$days\n"; } __DATA__ 1996 1 1996 2 1998 2 2000 2 1900 2 1600 2 2001 2 2001 7 2001 12 2001 13 2001 0 [download]``` Update: Small fixes to code as recommended by tye

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-02-22 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (289 votes). Check out past polls.

Notices?