Velocity Reviews > newbie need help with displaying dates for a leap year

# newbie need help with displaying dates for a leap year

Sean
Guest
Posts: n/a

 04-01-2005

Hi

As part of my JavaScript course I have been asked to do a switch
statement which displays the days of the month when the user enters a
number, easy enough.

but for February I need to display two conditions, one if it is not a
leap year and another one if it is a leap year

I have figured out how to get the year, and have been instructed to
display one condition if the number divides evenly by 4 (leap year)
and another if it doesn't.

do I use a comparison operator or something to do this, I have no
idea?

also once I can do this and have the if...else statement working
correctly, how can I include this in the switch statement?

Thanks

var today = new Date();
var year = today.getYear();

if (year / 4 == 501.25) // is 501.25
alert("There are 28 days because it is NOT a leap year") //
doesn't divide evenly by 4, an integer
else
alert("There are 29 days because it is a leap year") // does
not divide evenly by 4, a floating point number

var names, months;

months = parseInt(prompt("Please enter the number of the month 1
through 12", ""));

switch (months)
{
case 1 : alert("January has 31 days in it");
break;
case 2 : alert("February has 28 days in it");
break;
case 3 : alert("March has 31 days in it");
break;
case 4 : alert("April has 30 days in it");
break;
case 5 : alert("May has 31 days in it");
break;
case 6 : alert("June has 30 days in it");
break;
case 7 : alert("July has 31 days in it");
break;
case 8 : alert("August has 31 days in it");
break;
case 9 : alert("September has 30 days in it");
break;
case 10 : alert("October has 31 days in it");
break;
case 11 : alert("November has 30 days in it");
break;
case 12 : alert("December has 31 days in it");
break;
try again!");
}

McKirahan
Guest
Posts: n/a

 04-01-2005
"Sean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Hi
>
> As part of my JavaScript course I have been asked to do a switch
> statement which displays the days of the month when the user enters a
> number, easy enough.
>
> but for February I need to display two conditions, one if it is not a
> leap year and another one if it is a leap year
>
> I have figured out how to get the year, and have been instructed to
> display one condition if the number divides evenly by 4 (leap year)
> and another if it doesn't.
>
> do I use a comparison operator or something to do this, I have no
> idea?
>
> also once I can do this and have the if...else statement working
> correctly, how can I include this in the switch statement?
>
> Thanks

[snip]

Will this help? Watch for word-wrap.

alert( daysInMo(2,2004) + " days." );

function daysInMo(mo,yr) {
mo--;
return ( 28 | ( mo^1 && ++mo>>3^mo|2 || !(yr&3 || yr&15 && !(yr%25))) );
}

Adapted from a post by John Stockton.

Dr John Stockton
Guest
Posts: n/a

 04-01-2005
JRS: In article <(E-Mail Removed)>, dated
Fri, 1 Apr 2005 17:52:07, seen in news:comp.lang.javascript, Sean
<(E-Mail Removed)> posted :

>As part of my JavaScript course I have been asked to do a switch
>statement which displays the days of the month when the user enters a
>number, easy enough.
>
>but for February I need to display two conditions, one if it is not a
>leap year and another one if it is a leap year
>
>I have figured out how to get the year, and have been instructed to
>display one condition if the number divides evenly by 4 (leap year)
>and another if it doesn't.

In that case, your instructor is incompetent or misreported, since those
are not the current Leap Year rules, although satisfactory for
1901-2099. Still, as you post from a country that did not really exist
before 1901 ... .

>do I use a comparison operator or something to do this, I have no
>idea?
>
>also once I can do this and have the if...else statement working
>correctly, how can I include this in the switch statement?

case 2 : alert("February has " +
(28 + (year%4==0)) + " days in it");
break;

for the incorrect calendar specified;

case 2 : alert("February has " +
new Date(year, 2, 0).getDate() + " days in it");
break;

if you are prepared to use the correct calendar rules. The latter will
work for any value of 2 that you need; but for other months the result
is fixed.

>var today = new Date();
>var year = today.getYear();
>

In some systems. But not necessarily in all. Read the newsgroup FAQ;
see below.

>if (year / 4 == 501.25) // is 501.25
> alert("There are 28 days because it is NOT a leap year") //
>doesn't divide evenly by 4, an integer
>else
> alert("There are 29 days because it is a leap year") // does
>not divide evenly by 4, a floating point number

That will give the correct answer for 2005, and for every leap year.
All numbers in current javascript are floating point; they are IEEE
Doubles. Some of them have integer values, some have non-integer
values; a few have values which are not strictly numbers.

>months = parseInt(prompt("Please enter the number of the month 1
>through 12", ""));

Don't allow your posting agent to wrap code lines.
Don't use parseInt with only one parameter, until you understand exactly
when it is appropriate to do so; for this job, don't use parseInt.

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

Dr John Stockton
Guest
Posts: n/a

 04-01-2005
JRS: In article <(E-Mail Removed)>, dated Fri, 1 Apr
2005 06:34:45, seen in news:comp.lang.javascript, McKirahan
<(E-Mail Removed)> posted :
>"Sean" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .

>> As part of my JavaScript course I have been asked to do a switch
>> statement which displays the days of the month when the user enters a
>> number, easy enough.
>>
>> but for February I need to display two conditions, one if it is not a
>> leap year and another one if it is a leap year
>>
>> I have figured out how to get the year, and have been instructed to
>> display one condition if the number divides evenly by 4 (leap year)
>> and another if it doesn't.
>>
>> do I use a comparison operator or something to do this, I have no
>> idea?
>>
>> also once I can do this and have the if...else statement working
>> correctly, how can I include this in the switch statement?

>Will this help? Watch for word-wrap.
>
>alert( daysInMo(2,2004) + " days." );
>
>function daysInMo(mo,yr) {
> mo--;
> return ( 28 | ( mo^1 && ++mo>>3^mo|2 || !(yr&3 || yr&15 && !(yr%25))) );
>}
>
>Adapted from a post by John Stockton.

You have a nasty habit, now in this newsgroup as well as elsewhere, of
jumping in quickly with an ill-considered answer which frequently needs
correction. Perhaps eventually you will learn better.

The OP does not want to calculate the lengths of all of the months, only
of February; and he is *required* to use a switch statement. Therefore,
your response is of little use.

The code that you show uses the actual calendar, not the one specified.

The code that you show is unreasonably complex for a newbie of limited
knowledge and ability; it would only be justified if speed were of the
essence, or if the intention was to get the OP to hand in code that he
could not have written, or as an intellectual exercise among those
considerably more advanced (that algorithm is not mine).

=====

If speed is significant, compare
new Date(Date.UTC(year, month, 0)).getUTCDate()
& new Date( year, month, 0 ).getDate()

The latter is probably what would have been generally recommended in
this newsgroup for the length of a general Gregorian month. In my
MSIE4, the former is five or more times faster than the latter (the one
you quoted is five or more times faster than that).

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

RobG
Guest
Posts: n/a

 04-02-2005
Dr John Stockton wrote:
[...]
>
> In that case, your instructor is incompetent or misreported, since those
> are not the current Leap Year rules, although satisfactory for
> 1901-2099. Still, as you post from a country that did not really exist
> before 1901 ... .

Opinions on when Australia became a country differ.

The The Royal Commission of Assent to the Commonwealth of Australia
Constitution Act 1900, which provided for the creation of the
Commonwealth of Australia and enacted the Australian Constitution,
was signed (and therefore brought into force) by Queen Victoria on
9 July 1900.

The inauguration of the Commonwealth of Australia was celebrated on
1 January, 1901 which marks the official date of the federation of
the states. I presume that this is the date you are referring to.

However, this date is totally ignored by Australians: Australia Day
is celebrated on 26 January, which is the anniversary of 1788 landing
of Captain Arthur Phillip, who took formal possession of the colony
of New South Wales and became its first Governor.

It was called 'Foundation Day' and was officially recognised on the
30th anniversary of the landing in 1818. It was made a public
holiday in 1838.

The Australian federal and state governments did not agree on a
single date for the celebration of 'Australia Day' until 1946.

Many others believe that the above dates mark a history of
occupation and colonisation by a foreign power of what was an
existing, populated country.

To presume that Australia, as a country, did not exist before 1901
is, well, presumptuous.

--
Rob

Dr John Stockton
Guest
Posts: n/a

 04-02-2005
01.iinet.net.au>, dated Sat, 2 Apr 2005 15:31:16, seen in
news:comp.lang.javascript, RobG <(E-Mail Removed)> posted :
>Dr John Stockton wrote:
>[...]
>>
>> In that case, your instructor is incompetent or misreported, since those
>> are not the current Leap Year rules, although satisfactory for
>> 1901-2099. Still, as you post from a country that did not really exist
>> before 1901 ... .

>
> Opinions on when Australia became a country differ.
>

> To presume that Australia, as a country, did not exist before 1901
> is, well, presumptuous.

exist != really exist

--
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Evertjan.
Guest
Posts: n/a

 04-03-2005
Dr John Stockton wrote on 02 apr 2005 in comp.lang.javascript:

>> To presume that Australia, as a country, did not exist before 1901
>> is, well, presumptuous.

>
> exist != really exist

I presume is not really the definition of "exist",
but of "country", that devides you two.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)