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 6 of 6
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    877
    Thanks
    53
    Thanked 6 Times in 5 Posts

    crossbrowser date and time

    Hi. This works correctly in IE, but the year won't show in Firefox:

    var date;
    var day_array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var month_array = new Array("January","February","March","April","May","June","July","August","September","October","Novem ber","December");
    function show_time()
    {
    date = new Date();
    day = day_array[date.getDay()];
    maand = month_array[date.getMonth()];
    yand = date.getFullYear();
    minutes = date.getMinutes();
    seconds = date.getSeconds();
    if (minutes < 10 ) {minutes = "0" + minutes;}
    if (seconds < 10) {seconds = "0" + seconds;}
    obj_table_time = document.getElementById("table_time");
    obj_table_time.innerHTML = day + " "+date.getDate()+" " + maand + " " + yand + " "+date.getHours() + ":" + minutes +":" + seconds;
    setTimeout("show_time()",1000);
    }


    ********************************
    That variable yand doesn't do anything in Mozilla.

    Does anyone have any idea why?

    Thanks

  • #2
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    877
    Thanks
    53
    Thanked 6 Times in 5 Posts
    and here's another very weird thing that might be a bug in this forum(???).

    See the space in the word "Novem ber".

    I didn't post a space - and I double checked that I didn't post that space!

    Strange...

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You were using date as a variable. Firefox didn't like that because Date is reserved word. This works and it uses DOM to put the time in.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    var day_array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var month_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
    function show_time()
    {
    var thedate = new Date();
    var day = day_array[thedate.getDay()];
    var maand = month_array[thedate.getMonth()];
    var yand = thedate.getFullYear();
    var minutes = thedate.getMinutes();
    var seconds = thedate.getSeconds();
    if (minutes < 10 ) {minutes = "0" + minutes;}
    if (seconds < 10) {seconds = "0" + seconds;}
    var obj_table_time = document.getElementById("table_time");
    obj_table_time.firstChild.nodeValue = day + " "+thedate.getDate()+" " + maand + " " + yand + " "+thedate.getHours() + ":" + minutes +":" + seconds;
    }
    window.onload = function()
    {
    	show_time();
    	setInterval("show_time()",1000);
    }
    </script>
    </head>
    
    <body>
    <div id="table_time">&nbsp;</div>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts
    _Aerospace_Eng_:
    You may be right, but I can get the original script to work in FF and IE with only the following change:
    PHP Code:
    <body onLoad="show_time()"
    I think 'date' and 'Date' are different to all javascript.

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by jmrker
    _Aerospace_Eng_:
    You may be right, but I can get the original script to work in FF and IE with only the following change:
    I think 'date' and 'Date' are different to all javascript.
    Hmm not sure why the year didn't work then though it doesn't hurt to not use variables that have a reserved word in JS.
    Last edited by _Aerospace_Eng_; 08-31-2006 at 02:58 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    877
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I was using the body onload method, but the year wouldn't show in Firefox. (Everything else did). The whole thing displays if I change "date" to "thedate", so that'll do me.

    Thanks a lot.

    Still don't know about that interloping space in my first post! I reckon that sort of thing happens a lot in Outlook too. I type something, read it, send it, and... a single character will sometimes just vanish! Of course, it could be early senility. But that space before proves I'm not mad just yet.


  •  

    Posting Permissions

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