0
<html> 
    <p id="time"></p> 
  
    <script> 
      var theDate = new Date(Date.parse( 
        '06/14/2020 9:41:48 PM UTC')); 

 document.getElementById("time") 
        .innerHTML = theDate.toLocaleString(); 
    </script> 
</body> 
</html> 

When I remove the date which is "06/14/2020" in this example it tells me "Invalid date". I just want to show the time alone, without the date. How can I do this?

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117

2 Answers2

0

To print only the time, use toLocaleTimeString.

Nore about Datefunction you can find at Mozilla documentation

Jens
  • 60,806
  • 15
  • 81
  • 95
0

Do it that way

var interval = setInterval(currentTime,1000);
function currentTime(){
    var time = new Date();
    document.getElementById("time").innerHTML = time.toLocaleTimeString();
}
<p id="time"></p>