www.webdeveloper.com
Results 1 to 5 of 5

Thread: What is the problem with this date code

  1. #1
    Join Date
    Feb 2012
    Posts
    6

    What is the problem with this date code

    I need help with the following code. I am trying to show the day of the week together with the day, month and year

    var currentDate = new Date()
    var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
    "Thursday","Friday","Saturday")
    var now
    var ken = dayNames[now.getDay()]
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    document.write("<b>" ken +" " + day + "/" + month + "/" + year + "</b>")

    thank you

  2. #2
    Join Date
    Nov 2007
    Posts
    425
    Fixed code here:
    Code:
    <script>
    var currentDate = new Date();
    var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var now = new Date();
    var ken = dayNames[now.getDay()];
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();
    document.write("<b>" + ken +" " + day + "/" + month + "/" + year + "</b>");
    </script>
    The problems with your code:
    1- lack of semi-colons. Yes, your code will work in most browser, but you should use them anyway.
    2- "var now" but you never say what "now" is. Since "currentDate" is already today, you really don't need a variable for "now", but if you want it, you need to say "var now = new Date()".
    3- Missing a "+" in your document.write line here: "<b>" ken +

    Fixed code without the superfluous "now" variable.
    Code:
    <script>
    var currentDate = new Date();
    var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var ken = dayNames[currentDate.getDay()];
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();
    document.write("<b>" + ken +" " + day + "/" + month + "/" + year + "</b>");
    </script>

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    1) The semi-colon at the end of a line is optional according to the definition of the language. It's not browser dependent and you can use it or not as you like. I recommend omitting it whenever possible so as to irk certain people.
    2) The "type" attribute, however, is required with the SCRIPT element.
    3) Document.write takes a variable number of arguments and concatenates them. So you could omit the "+" things and use document.write("<b>", ken, " ", day, "/", month, "/", year, "</b>").
    4) Except that you shouldn't use the B element here nor should you use document.write. Best to use STRONG and the DOM.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Feb 2012
    Posts
    6
    Thank you napOlean:

    It worked fine with your comments.

  5. #5
    Join Date
    Feb 2012
    Posts
    6
    Thank you charles:

    I also tried omitting the "+" and that also worked fine. Same is true with omitting semicolons. I did not understand the B element, but since i am a biginner, i will come to it as i work more.

Thread Information

Users Browsing this Thread

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

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