+ 2
What's wrong with this code not showing session
function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); var s = ["AM","PM"]; if(hours > 12){ document.write(s[1]); } document.body.innerHTML = hours+":"+mins+":"+secs+":"+s; } setInterval(printTime, 1000);
4 Respuestas
+ 1
function printTime() {
var d = new Date();
var hours = d.getHours();
var mins = d.getMinutes();
var secs = d.getSeconds();
var s = hours >= 12 ? "PM" : "AM"; //use this way, ternary operator
document.body.innerHTML = hours+":"+mins+":"+secs+":"+s;
}
setInterval(printTime, 1000);
+ 1
Jayakrishna🇮🇳 can I use array?
+ 1
Yes. You can
function printTime() {
var d = new Date();
var hours = d.getHours();
var mins = d.getMinutes();
var secs = d.getSeconds();
var s = [ "AM", " PM" ];
document.body.innerHTML = hours+":"+mins+":"+secs+":"+ ( hours >= 12 ? s[1] : s[0] ) ;
}
setInterval(printTime, 1000);
//but see you are using extra variable s..!
+ 1
Jayakrishna🇮🇳 thanks for your help 😘😘