javascriptdatetimetimeofday

How to just show the time not the time AND date?


<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?


Solution

  • To print only the time, use toLocaleTimeString.

    Nore about Datefunction you can find at Mozilla documentation