Show in real time, date, day of the week in javascript or jquery

How to show in real time, date, day in this format?

enter image description here

The time should be current (which counts the number of seconds).

Thanks guys!

+6
source share
3 answers

To update the timebar every second, we must use the setInterval() function.

To format the date as best suits you, use the moment.js library. The code is greatly reduced:

 $(document).ready(function() { var interval = setInterval(function() { var momentNow = moment(); $('#date-part').html(momentNow.format('YYYY MMMM DD') + ' ' + momentNow.format('dddd') .substring(0,3).toUpperCase()); $('#time-part').html(momentNow.format('A hh:mm:ss')); }, 100); }); 

The violin works here

+14
source

You can use SetInterval in javascript and run it every 1 second. Find an example

 <!DOCTYPE html> <html> <body> <p>A script on this page starts this clock:</p> <p id="demo"></p> <p id="demonew"></p> <script> var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; //January is 0! var yyyy = today.getFullYear(); if(dd<10) { dd='0'+dd } if(mm<10) { mm='0'+mm } today = mm+'/'+dd+'/'+yyyy; document.getElementById("demonew").innerHTML = today; var myVar=setInterval(function(){myTimer()},1000); function myTimer() { var d = new Date(); document.getElementById("demo").innerHTML = d.toLocaleTimeString(); } </script> </body> </html> 
+1
source
 You can use : <script> var mydate=new Date() var year=mydate.getYear() if (year < 1000) year+=1900 var day=mydate.getDay() // Current Day of week - 2 var month=mydate.getMonth() // Current Month 2 var daym=mydate.getDate() // Current Date -24 var h=mydate.getHours(); //Hours var m=mydate.getMinutes();//Minutes var s=mydate.getSeconds();//Seconds m = checkTime(m); s = checkTime(s); function checkTime(i) { if (i<10) {i = "0" + i}; // add zero in front of numbers < 10 return i; } 

Take 2 Arrays

 var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday") var montharray=new Array("January","February","March","April","May","June", "July","August","September","October","November","December") document.getElementById('txt').innerHTML =h+":"+m+":"+s+" "+dayarray[day]+", "+montharray[month]+" "+daym+", "+year; var t = setTimeout(function(){startTime()},500); //This will update time </script> <body onload="startTime()"> <div id="txt"></div> </body> 
+1
source

Source: https://habr.com/ru/post/982927/


All Articles