Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    having trouble getting a date

    Someone tell me where I'm going wrong. I'm trying to print out the month/year ?

    Code:
    <html><head>
    <SCRIPT language=JavaScript>
    var dateNow = new Date();
    var monthNow = dateNow.getMonth();
    var yearNow = dateNow.getYear();
    function MakeArray(n) {
    this.length = n
    return this
    } 
    monthNames = new MakeArray(12)
    monthNames[1] = "Janurary"
    monthNames[2] = "February"
    monthNames[3] = "March"
    monthNames[4] = "April"
    monthNames[5] = "May"
    monthNames[6] = "June"
    monthNames[7] = "July"
    monthNames[8] = "August"
    monthNames[9] = "Sept."
    monthNames[10] = "Oct."
    monthNames[11] = "Nov."
    monthNames[12] = "Dec."
    function customDate(oneDate) {
    var theMonth = monthNames[oneDate.getMonth() +1]
    }
    </SCRIPT>
    </head><body><script type="text/javascript">
    document.write(theMonth + "/" + yearNow);
    </script>
    </body></html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,370 Times in 4,335 Posts
    Have you tried eHarmony.com???

    (Sorry, couldn't resist.)

    You have NEVER CALL your customDate function.

    And too much code. K.I.S.S.:
    Code:
    <html><head>
    <script type="text/javascript">
    var monthNames = [ "January","February","March","April","May","June","July",
                       "August","September","October","November","December"     ];
    var today = new Date();  
    var mmyy = monthName[today.getMonth()] + "/" + today.getYear();
    </script>
    </head>
    <body>
    <script type="text/javascript">document.write(mmyy);</script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thanks for the response. You probably know a novice by the volume of code. I get two errors: unterminated string constant @ line 3 and undefined 'mmyy' @ line 8 ?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,370 Times in 4,335 Posts
    You are missing a " in line 3 and that then causes the problem in line 8.

    And I missed the "s" on the end of monthNames in line 6.

    Check the code carefully.

    Note that this MIGHT give you an answer of "September/110" because of the use of getYear().

    You might want to use getFullYear() to produce "September/2010". If you really need "September/10" then I'd use getFullYear() % 100.

    So:
    Code:
    <html><head>
    <script type="text/javascript">
    var monthNames = [ "January","February","March","April","May","June","July",
                       "August","September","October","November","December"     ];
    var today = new Date();  
    var mmyy = monthNames[today.getMonth()] + "/" + today.getFullYear() % 100;
    </script>
    </head>
    <body>
    <script type="text/javascript">document.write(mmyy);</script>
    </body>
    </html>
    where the % 100 is up to you.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    ckdoublenecks (09-02-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •