newbie need help with displaying dates for a leap year

S

Sean

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();

alert(year) // displays 2005


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;
default : alert("You entered some other information, please
try again!");
}
 
M

McKirahan

Sean said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Fri, 1 Apr 2005 17:52:07, seen in Sean
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();

alert(year) // displays 2005

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.
Read the newsgroup FAQ.


Read the newsgroup FAQ.


Read the newsgroup FAQ.


And ask your instructor to read the newsgroup FAQ.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 1 Apr
2005 06:34:45, seen in McKirahan
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).
 
R

RobG

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.
 
D

Dr John Stockton

JRS: In article <424e2f5e$0$1873$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 2 Apr 2005 15:31:16, seen in
news:comp.lang.javascript said:
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
 
E

Evertjan.

Dr John Stockton wrote on 02 apr 2005 in comp.lang.javascript:
exist != really exist

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top