www.webdeveloper.com
Results 1 to 3 of 3

Thread: Date Format Error:

  1. #1
    Join Date
    May 2010
    Posts
    36

    Date Format Error:

    I am trying to format a date as say(May 12 2010) But My output is undefined 12 2010.

    here is my code, Can you help me know where I am going wrong here:

    Code:
    <html>
    
    <head>
    
    <title>exercise 1</title>
    <script type="text/javascript">
    var dt = new Date();
    var m_names =new Array{"January", "Feberuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    var month = dt.getMonth()+1;
    var day = dt.getDate();
    var year = dt.getFullYear();
    //dateFormat(dt, "mmm, dS, yyyy");
    document.write(m_names[month] +"/" +day +"/" +year);
    
    </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    Thanks

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    You created the array incorrectly, and january is month[0],don't add 1 to getMonth().

    Code:
    var dt = new Date();
    var m_names =["January", "Feberuary", "March", "April", "May", "June", "July", 
    "August", "September", "October", "November", "December"];
    var month = dt.getMonth();
    var day = dt.getDate();
    var year = dt.getFullYear();
    
    document.write(m_names[month] +" " +day +", " +year);
    /* returned value: (String)
    May 12, 2010
    */
    Last edited by mrhoo; 05-12-2010 at 11:24 AM.

  3. #3
    Join Date
    May 2010
    Posts
    36
    Quote Originally Posted by mrhoo View Post
    You created the array incorrectly, and january is month[0],don't add 1 to getMonth().

    Code:
    var dt = new Date();
    var m_names =["January", "Feberuary", "March", "April", "May", "June", "July", 
    "August", "September", "October", "November", "December"];
    var month = dt.getMonth();
    var day = dt.getDate();
    var year = dt.getFullYear();
    
    document.write(m_names[month] +" " +day +", " +year);
    /* returned value: (String)
    May 12, 2010
    */

    Thanks for the help. I should have wrapped array objects within () rather {}

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles