Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Getting number of days in a month

Reply
Thread Tools

Getting number of days in a month

 
 
Dan Fitzpatrick
Guest
Posts: n/a
 
      02-27-2008
Shandy Nantz wrote:
> This is probably an easy question but I am trying to get at the number
> of days that are in a month. I have this calendar that I have built, the
> idea being that when a month turns from February to March, for example,
> the calendar should redisplay itself properly formated showing the new
> month and the correct number of days. I have it so that it starts
> counting the days on the right day of the week, but I have to know when
> to stop counting. Any ideas, Thanks,
>
> -S
>

If you don't want to use the Date module, this is the plain old way:

def days_in_month(m=Time.new.month,y=Time.new.year)
return [31,0,31,30,31,30,31,31,30,31,30,31][m-1] unless m == 2
((y % 4 == 0) and ( (!(y % 100 == 0)) or (y % 400 == 0) ) ) ? 29 : 28
end

Dan



 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      02-27-2008

On Feb 27, 2008, at 2:17 PM, Gregory Seidman wrote:
> On Thu, Feb 28, 2008 at 03:29:54AM +0900, Brian Adkins wrote:
>> On Feb 27, 1:00 pm, Gregory Seidman <gsslist+r...@anthropohedron.net>
>> wrote:
>>> On Thu, Feb 28, 2008 at 02:49:54AM +0900, Brian Adkins wrote:
>>>> On Feb 27, 11:40 am, Gregory Seidman <gsslist
>>>> +r...@anthropohedron.net>
>>>> wrote:
>>>>> On Thu, Feb 28, 2008 at 01:17:22AM +0900, Shandy Nantz wrote:
>>>>>> This is probably an easy question but I am trying to get at the
>>>>>> number
>>>>>> of days that are in a month. I have this calendar that I have
>>>>>> built, the
>>>>>> idea being that when a month turns from February to March, for
>>>>>> example,
>>>>>> the calendar should redisplay itself properly formated showing
>>>>>> the new
>>>>>> month and the correct number of days. I have it so that it starts
>>>>>> counting the days on the right day of the week, but I have to
>>>>>> know when
>>>>>> to stop counting. Any ideas, Thanks,
>>>
>>>>> {code removed}


>>>>>> -S
>>>>> --Greg
>>>
>>>> might want to try running that before posting
>>>
>>> Ah, details. Change the last line of the method to:
>>>
>>> (next_month + next_month.mday).mday
>>>
>>> Anyhow, it's worth noting that ActiveSupport includes
>>> Time.days_in_month.
>>>
>>> --Greg

>>
>> You still didn't run it, did you? <sigh>

>
> Don't sigh at me. I wrote some code off the cuff and fired it off. I
> also
> didn't include unit tests. Yes, it was buggy and inefficient, but it
> got
> across the approach I was using.



The point is applicable to *everyone*:

"Don't present code that you haven't actually
run unless you *say* that you haven't run it."

You'll just end up confusing the OP or other readers that may benefit
from
the code. If you're trying to illustrate a point or an approach, say so
or make it quite clear that your code is incomplete or intended as
pseudo-code (particularly if it appears to be valid syntax).

-Rob

Rob Biedenharn http://agileconsultingllc.com




 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      02-27-2008
Gregory Seidman wrote:
> Don't sigh at me. I wrote some code off the cuff and fired it off. I
> also
> didn't include unit tests. Yes, it was buggy and inefficient, but it got
> across the approach I was using.
>


Thanks for caring, however this isn't a creative writing forum.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
James Britt
Guest
Posts: n/a
 
      02-27-2008
Rob Biedenharn wrote:

>
> The point is applicable to *everyone*:
>
> "Don't present code that you haven't actually
> run unless you *say* that you haven't run it."
>


Nah, better to let dubious code pop up now and then so that people are
not lulled into a false sense of security, and will hopefully realize
that, even if someone says they ran and tested the code and assure you
it's fine, the code may still be bad.



--
James Britt

"In physics the truth is rarely perfectly clear, and that is certainly
universally the case in human affairs. Hence, what is not surrounded by
uncertainty cannot be the truth."
- R. Feynman

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      02-28-2008
On Wed, Feb 27, 2008 at 2:14 PM, Rob Biedenharn
<> wrote:
> The point is applicable to *everyone*:
>
> "Don't present code that you haven't actually
> run unless you *say* that you haven't run it."
>
> You'll just end up confusing the OP or other readers that may benefit
> from
> the code. If you're trying to illustrate a point or an approach, say so
> or make it quite clear that your code is incomplete or intended as
> pseudo-code (particularly if it appears to be valid syntax).
>
> -Rob


I frequently write bad code when I use a system where I cannot cut and
paste. All code on a forum, IMHO, should be taken with a grain of
salt. I don't think posted code needs to be perfect.

2c,
Todd

 
Reply With Quote
 
Shandy Nantz
Guest
Posts: n/a
 
      02-28-2008
Morton Goldberg wrote:
> <code>
> require "Date"
> d = Date.new(2008, 2, -1)
> d.day # => 29
> </code>
> Regards, Morton


This is what I ened up using and it seems to work. Thanks,

-S
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      02-28-2008
On Feb 27, 10:17 am, Shandy Nantz <shandyb...@yahoo.com> wrote:
> This is probably an easy question but I am trying to get at the number
> of days that are in a month. I have this calendar that I have built, the
> idea being that when a month turns from February to March, for example,
> the calendar should redisplay itself properly formated showing the new
> month and the correct number of days. I have it so that it starts
> counting the days on the right day of the week, but I have to know when
> to stop counting. Any ideas, Thanks,
>
> -S
> --
> Posted viahttp://www.ruby-forum.com/.


Awk (not my code):

function days_in_month( y, m )
{
return m==2 ? 28+!(y%4)-!(y%100)+!(y%400) : 30+(m%2 != (m>7))
}
 
Reply With Quote
 
Serg Koren
Guest
Posts: n/a
 
      02-28-2008
A more interesting question is how do you get the number of weeks in a
year.
Some years have 52...some have 54. Calendar weeks...not 7 day weeks.

S




On Feb 28, 2008, at 5:20 PM, William James wrote:

> On Feb 27, 10:17 am, Shandy Nantz <shandyb...@yahoo.com> wrote:
>> This is probably an easy question but I am trying to get at the
>> number
>> of days that are in a month. I have this calendar that I have
>> built, the
>> idea being that when a month turns from February to March, for
>> example,
>> the calendar should redisplay itself properly formated showing the
>> new
>> month and the correct number of days. I have it so that it starts
>> counting the days on the right day of the week, but I have to know
>> when
>> to stop counting. Any ideas, Thanks,
>>
>> -S
>> --
>> Posted viahttp://www.ruby-forum.com/.

>
> Awk (not my code):
>
> function days_in_month( y, m )
> {
> return m==2 ? 28+!(y%4)-!(y%100)+!(y%400) : 30+(m%2 != (m>7))
> }
>



 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      02-29-2008
On 2/28/08, Serg Koren <> wrote:
> A more interesting question is how do you get the number of weeks in a
> year.
> Some years have 52...some have 54. Calendar weeks...not 7 day weeks.


I assume you mean here how many 7 day weeks starting on a particular
week day have at least one day in a given year.

If this is the case then in all but one case every year has 53
'weeks.' The only exception is a leap year which starts on the last
day of the week which has 54 weeks.

for start in (1..7) do
for days in (365..366) do
s = "a #{{365 => 'normal', 366 => 'leap'}[days]} year starting on
weekday #{start} starts with "
start_week_days = start == 1 ? 0 : (8 - start)
s << "a 'week' of #{start_week_days} days then " unless start_week_days == 0
days_remaining = days - start_week_days
full_weeks = days_remaining / 7
end_week_days = days_remaining % 7
s << "#{full_weeks} full weeks"
s << " and ends with a 'week' of #{end_week_days} days" unless
end_week_days == 0
s << " a total of #{(start_week_days > 0 ? 1 : 0) + full_weeks +
(end_week_days > 0 ? 1 : 0)} 'weeks.'"
puts s
end
puts
end


a normal year starting on weekday 1 starts with 52 full weeks and ends
with a 'week' of 1 days a total of 53 'weeks.'
a leap year starting on weekday 1 starts with 52 full weeks and ends
with a 'week' of 2 days a total of 53 'weeks.'

a normal year starting on weekday 2 starts with a 'week' of 6 days
then 51 full weeks and ends with a 'week' of 2 days a total of 53
'weeks.'
a leap year starting on weekday 2 starts with a 'week' of 6 days then
51 full weeks and ends with a 'week' of 3 days a total of 53 'weeks.'

a normal year starting on weekday 3 starts with a 'week' of 5 days
then 51 full weeks and ends with a 'week' of 3 days a total of 53
'weeks.'
a leap year starting on weekday 3 starts with a 'week' of 5 days then
51 full weeks and ends with a 'week' of 4 days a total of 53 'weeks.'

a normal year starting on weekday 4 starts with a 'week' of 4 days
then 51 full weeks and ends with a 'week' of 4 days a total of 53
'weeks.'
a leap year starting on weekday 4 starts with a 'week' of 4 days then
51 full weeks and ends with a 'week' of 5 days a total of 53 'weeks.'

a normal year starting on weekday 5 starts with a 'week' of 3 days
then 51 full weeks and ends with a 'week' of 5 days a total of 53
'weeks.'
a leap year starting on weekday 5 starts with a 'week' of 3 days then
51 full weeks and ends with a 'week' of 6 days a total of 53 'weeks.'

a normal year starting on weekday 6 starts with a 'week' of 2 days
then 51 full weeks and ends with a 'week' of 6 days a total of 53
'weeks.'
a leap year starting on weekday 6 starts with a 'week' of 2 days then
52 full weeks a total of 53 'weeks.'

a normal year starting on weekday 7 starts with a 'week' of 1 days
then 52 full weeks a total of 53 'weeks.'
a leap year starting on weekday 7 starts with a 'week' of 1 days then
52 full weeks and ends with a 'week' of 1 days a total of 54 'weeks.'

Or did you mean something else.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

 
Reply With Quote
 
Serg Koren
Guest
Posts: n/a
 
      02-29-2008
Good job. That's exactly what I meant.



On Feb 28, 2008, at 9:01 PM, Rick DeNatale wrote:

> On 2/28/08, Serg Koren <> wrote:
>> A more interesting question is how do you get the number of weeks
>> in a
>> year.
>> Some years have 52...some have 54. Calendar weeks...not 7 day weeks.

>
> I assume you mean here how many 7 day weeks starting on a particular
> week day have at least one day in a given year.
>
> If this is the case then in all but one case every year has 53
> 'weeks.' The only exception is a leap year which starts on the last
> day of the week which has 54 weeks.
>
> for start in (1..7) do
> for days in (365..366) do
> s = "a #{{365 => 'normal', 366 => 'leap'}[days]} year starting on
> weekday #{start} starts with "
> start_week_days = start == 1 ? 0 : (8 - start)
> s << "a 'week' of #{start_week_days} days then " unless
> start_week_days == 0
> days_remaining = days - start_week_days
> full_weeks = days_remaining / 7
> end_week_days = days_remaining % 7
> s << "#{full_weeks} full weeks"
> s << " and ends with a 'week' of #{end_week_days} days" unless
> end_week_days == 0
> s << " a total of #{(start_week_days > 0 ? 1 : 0) + full_weeks +
> (end_week_days > 0 ? 1 : 0)} 'weeks.'"
> puts s
> end
> puts
> end
>
>
> a normal year starting on weekday 1 starts with 52 full weeks and ends
> with a 'week' of 1 days a total of 53 'weeks.'
> a leap year starting on weekday 1 starts with 52 full weeks and ends
> with a 'week' of 2 days a total of 53 'weeks.'
>
> a normal year starting on weekday 2 starts with a 'week' of 6 days
> then 51 full weeks and ends with a 'week' of 2 days a total of 53
> 'weeks.'
> a leap year starting on weekday 2 starts with a 'week' of 6 days then
> 51 full weeks and ends with a 'week' of 3 days a total of 53 'weeks.'
>
> a normal year starting on weekday 3 starts with a 'week' of 5 days
> then 51 full weeks and ends with a 'week' of 3 days a total of 53
> 'weeks.'
> a leap year starting on weekday 3 starts with a 'week' of 5 days then
> 51 full weeks and ends with a 'week' of 4 days a total of 53 'weeks.'
>
> a normal year starting on weekday 4 starts with a 'week' of 4 days
> then 51 full weeks and ends with a 'week' of 4 days a total of 53
> 'weeks.'
> a leap year starting on weekday 4 starts with a 'week' of 4 days then
> 51 full weeks and ends with a 'week' of 5 days a total of 53 'weeks.'
>
> a normal year starting on weekday 5 starts with a 'week' of 3 days
> then 51 full weeks and ends with a 'week' of 5 days a total of 53
> 'weeks.'
> a leap year starting on weekday 5 starts with a 'week' of 3 days then
> 51 full weeks and ends with a 'week' of 6 days a total of 53 'weeks.'
>
> a normal year starting on weekday 6 starts with a 'week' of 2 days
> then 51 full weeks and ends with a 'week' of 6 days a total of 53
> 'weeks.'
> a leap year starting on weekday 6 starts with a 'week' of 2 days then
> 52 full weeks a total of 53 'weeks.'
>
> a normal year starting on weekday 7 starts with a 'week' of 1 days
> then 52 full weeks a total of 53 'weeks.'
> a leap year starting on weekday 7 starts with a 'week' of 1 days then
> 52 full weeks and ends with a 'week' of 1 days a total of 54 'weeks.'
>
> Or did you mean something else.
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denhaven2.com/
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Convert month name to month number faster Steven D'Aprano Python 0 01-06-2010 12:48 PM
RE: Convert month name to month number faster VYAS ASHISH M-NTB837 Python 0 01-06-2010 11:14 AM
Figure out month number from month abbrievation Bill Python 10 04-13-2006 12:36 PM
Return Number of Days In a month. Adam Knight ASP General 5 05-19-2004 09:09 PM
Date Dropdown entry - Adjusting number of days based on month wrecker ASP .Net Web Controls 2 02-05-2004 10:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57