SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You should be using getFullYear instead of getYear because getYear has a Y2K bug and will display 110 in some browsers.

    Also why have a blank entry at the front of the array and have to add one to the month to find the right entry. Simpy assign

    var monthnames = ['January','February', ...

    and get rid of the +1.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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