SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Current Date Update in Browser?

    I've implemented a script that shows the current time and date.
    The issue is when the users browser windows remains open, the time does not update every 60 seconds on the minute change etc. Is there a way to have the time/script reload to reflect the current script or would this be better suited to something server side? Thank you so much!



    <!--

    var a_p = "";
    var d = new Date();
    var curr_hour = d.getHours();
    if (curr_hour < 12)
    {
    a_p = "AM";
    }
    else
    {
    a_p = "PM";
    }
    if (curr_hour == 0)
    {
    curr_hour = 12;
    }
    if (curr_hour > 12)
    {
    curr_hour = curr_hour - 12;
    }

    var curr_min = d.getMinutes();

    curr_min = curr_min + "";

    if (curr_min.length == 1)
    {
    curr_min = "0" + curr_min;
    }

    document.write(curr_hour + " : " + curr_min + " " + a_p + "&nbsp;");

    //-->

    <!--

    var months = new Array(12);
    months[0] = "January";
    months[1] = "February";
    months[2] = "March";
    months[3] = "April";
    months[4] = "May";
    months[5] = "June";
    months[6] = "July";
    months[7] = "August";
    months[8] = "September";
    months[9] = "October";
    months[10] = "November";
    months[11] = "December";

    var current_date = new Date();
    month_value = current_date.getMonth();
    day_value = current_date.getDate();
    year_value = current_date.getFullYear();

    document.write("" + months[month_value] + " " +
    day_value + ", " + year_value);

    //-->

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,817
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    This is something that cannot be done server side - you can only do it with JavaScript.

    It also cannot be done using document.write() as that only substitutes for server side code when you don't have a server side language available.

    You will need to either use the standard DOM calls to update the page content to change the time or use innerHTML.
    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
  •