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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation how do you position a javascript date in a html table?

    So I've now made the layout of my website and I need to know how to position a javascript date I made, in a html table. This is the basic "var d=new date();" code I'm trying to implement in the menu section of my website. Also, I need to know how to do this using an external javascript file.

  • #2
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    Can you elaborate on what you want please?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Code:
    var months = ["Jan","Feb","Mar",...];
    function setDateInPlace( )
    {
        var now = new Date();
        var dateAsString = months[now.getMonth()] + " " + now.getDate() + ", " + now .getFullYear();
        document.getElementById("whereTheDateGoes").innerHTML = dateAsString;
    }
    window.onload = setDateInPlace(); // better would be to use attachEvent
    So make your HTML look pretty, maybe
    Code:
    <div id="whereTheDateGoes" class="someThingFancyNoDoubt">Feb 29, 2013</div>
    and then let the JS code replace the pretty date and the replacement will look similarly pretty.
    Last edited by Old Pedant; 03-29-2012 at 01:09 AM.
    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.

  • #4
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    Probably some mere quick-finger errors, but, on line 5, you're treating months as a function instead of an array. Also, in the HTML, the ID doesn't match the javascript target ID.
    Last edited by Nile; 03-29-2012 at 01:03 AM.

  • Users who have thanked Nile for this post:

    robe01 (03-29-2012)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Thanks. One wrong language and one slipped finger. Fixed.
    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:

    robe01 (03-29-2012)


  •  

    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
    •