Categories: javascript

how to edit the JS date and time to update atuomatically?

2 answers

I have got a very simple JS code the prints out Date and time / I'm good the the date part, the CLOCK thou doesn't update minutes automatically, how can I have it update minutes with out refreshing the page, NO NEED TO SHOW SECONDS / I just want to see minutes update automatically,

how can I alter this code to do that plz (I'm a beginner / please give me details)

    <script>         var dt = new Date();         document.getElementById("datendtime").innerHTML = dt.toLocaleString([], {             weekday: 'long',             month: '2-digit',             day: '2-digit',             year: '2-digit',             hour: '2-digit',             minute: '2-digit'         });     </script> 

HTML to print it:

<span id="datendtime"></span> 

All answers to this question, which has the identifier 61239418

The best answer:

As @Yishmeray said in the comments, you'll need to use something like setInterval which executes code at a set (although imprecise) interval.

Note that setInterval is not guaranteed to run at the exact interval you specify.

setInterval(() => {  	document.getElementById("datendtime").innerHTML = refreshDate(new Date())  }, 1000)        function refreshDate(date) {      return date.toLocaleString([], {        weekday: 'long',        month: '2-digit',        day: '2-digit',        year: '2-digit',        hour: '2-digit',        minute: '2-digit'      });    }
<div id="datendtime"></div>

var screen = document.querySelector("p");    setInterval(function() {    screen.innerHTML = new Date().toLocaleString([], {      weekday: 'long',      month: '2-digit',      day: '2-digit',      year: '2-digit',      hour: '2-digit',      minute: '2-digit'    });  }, 1000);
<p></p>

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data