0

I am trying to display the most current date and time that occurs every second but the output is staying the same. I dont want to use jquery because I am learning how to do this with javascript. The display function is being called by the body on onload.

function display()
{
    var today = new Date();
    var month = today.getMonth();
    var day = today.getDay();
    var year = today.getFullYear();

    var hour = today.getHours() > 12 ? today.getHours() - 12 : today.getHours();
    var minute = today.getMinutes();
    var seconds = today.getSeconds();
    var milliseconds = today.getMilliseconds();

    var output = month + '/' + day + '/' + year + ' - ' +
    hour + ':' + minute + ':' + seconds + ':' + milliseconds;

    setInterval(function() {

    document.write(output);

    }, 3000);
}
Exploit
  • 6,278
  • 19
  • 70
  • 103

4 Answers4

6

Try this :

function display()
{
    var today = new Date();
    var month = today.getMonth();
    var day = today.getDay();
    var year = today.getFullYear();

    var hour = today.getHours() > 12 ? today.getHours() - 12 : today.getHours();
    var minute = today.getMinutes();
    var seconds = today.getSeconds();
    var milliseconds = today.getMilliseconds();

    var output = month + '/' + day + '/' + year + ' - ' +
    hour + ':' + minute + ':' + seconds + ':' + milliseconds;

    document.write(output);
}
setInterval(display, 3000);

sidenote: Is document.write is what you want ? You probably want to display it within a <span> or a <div> . In this case, document.getElementById('someId').innerHTML helps .

another sidenote: setInterval(display, 3000); and setInterval('display();', 3000); is different. See this answer for more details.

Community
  • 1
  • 1
Raptor
  • 53,206
  • 45
  • 230
  • 366
2

You need to do the calculation inside the timer function, like this:

function display()
{
  setInterval(function() {
    var today = new Date();
    var month = today.getMonth();
    var day = today.getDay();
    var year = today.getFullYear();

    var hour = today.getHours() > 12 ? today.getHours() - 12 : today.getHours();
    var minute = today.getMinutes();
    var seconds = today.getSeconds();
    var milliseconds = today.getMilliseconds();

    var output = month + '/' + day + '/' + year + ' - ' +
    hour + ':' + minute + ':' + seconds + ':' + milliseconds;

    document.write(output);

  }, 3000);
}
display();
PowerPanda
  • 779
  • 5
  • 11
  • i tried that but what happens is that the loader keeps loading and nothing changes. – Exploit May 02 '13 at 07:16
  • you will also need to call the display() function somewhere, to set up the timer. Just call display() at the end of the function – PowerPanda May 02 '13 at 08:09
0

A value of a variable keeps static. So if you don't change the value of outout, it keeps the same.

So you need to calculate the date inside the interval callback and then display it.

Hariharan
  • 3,191
  • 4
  • 19
  • 31
Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • i tried moving everything inside setinterval and in firebug this is what i see: TypeError: document.getElementById(...) is null [Break On This Error] document.getElementById(output).innerHTML = output; – Exploit May 02 '13 at 07:29
0
import React from 'react'
import './App.css'

function App(){
 const [exactTime, set] = React.useState()
 function p ( ) {
    set (() => {
      const date = new Date ()

      return (
        date.getSeconds()
       )
    });

  }
  setInterval(p, 1000)

  return (

    <div className="App">

      <p>
        Time is { exactTime }
      </p>

    </div>
  );

}