newbie need help with displaying dates for a leap year

Discussion in 'Javascript' started by Sean, Apr 1, 2005.

  1. Sean

    Sean Guest

    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!");
    }
    Sean, Apr 1, 2005
    #1
    1. Advertising

  2. Sean

    McKirahan Guest

    "Sean" <> wrote in message
    news:...
    >
    > 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.
    McKirahan, Apr 1, 2005
    #2
    1. Advertising

  3. JRS: In article <>, dated
    Fri, 1 Apr 2005 17:52:07, seen in news:comp.lang.javascript, Sean
    <> 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();
    >
    >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.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <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.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Apr 1, 2005
    #3
  4. JRS: In article <>, dated Fri, 1 Apr
    2005 06:34:45, seen in news:comp.lang.javascript, McKirahan
    <> posted :
    >"Sean" <> wrote in message
    >news:...


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

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <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.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Apr 1, 2005
    #4
  5. Sean

    RobG Guest

    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
    RobG, Apr 2, 2005
    #5
  6. 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, RobG <> 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

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    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.
    Dr John Stockton, Apr 2, 2005
    #6
  7. Sean

    Evertjan. Guest

    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)
    Evertjan., Apr 3, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hai Nguyen

    Date Validation Expression and Leap Year

    Hai Nguyen, Feb 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,309
    Hans Kesting
    Feb 27, 2004
  2. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    839
    Rob Schieber
    Sep 30, 2005
  3. Wen
    Replies:
    4
    Views:
    460
    Simon Saunders
    Dec 12, 2003
  4. Protoman
    Replies:
    14
    Views:
    540
  5. HB
    Replies:
    9
    Views:
    149
    Hh Rg
    Jun 23, 2010
Loading...

Share This Page