window.onload = setInterval(function() {
let today = new Date();
let year = today.getFullYear();
let month = today.getMonth() + 1;
let day = today.getDate();
let hour = today.getHours();
let min = today.getMinutes();
let sec = today.getSeconds();
am_pm = "AM";
if (hour > 12) {
hour -= 12;
am_pm = "PM";
}
if (hour == 0) {
hour = 12;
am_pm = "AM";
}
hour = hour < 10 ? "0" + hour : hour;
min = min < 10 ? "0" + min : min;
sec = sec < 10 ? "0" + sec : sec;
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
let currentDate = [year, month, day].join("-")
let currentTime = hour + ":"
+ min + ":" + sec + " " + am_pm;
document.getElementById("app")
.innerHTML = currentDate + " " + currentTime;
}, 1000)
function currentTime() {
let date = new Date();
let hh = date.getHours();
let mm = date.getMinutes();
let ss = date.getSeconds();
let session = "AM";
if(hh == 0){
hh = 12;
}
if(hh > 12){
hh = hh - 12;
session = "PM";
}
hh = (hh < 10) ? "0" + hh : hh;
mm = (mm < 10) ? "0" + mm : mm;
ss = (ss < 10) ? "0" + ss : ss;
let time = hh + ":" + mm + ":" + ss + " " + session;
document.getElementById("clock").innerText = time;
let t = setTimeout(function(){ currentTime() }, 1000);
}
currentTime();
//you can use Timer to do yours clock
Timer timer=new Timer();
TimerTask task=new TimerTask(){
int h=0;
int s=0;
public void run(){
s++;
if(s==60){
h++;
s=0;
if(h==25){
h=0;
}
}
}
};
timer.schedule(task,0, 1000);
timer.schedule(task, 1000);
};