+ 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);

17th Jul 2022, 3:38 PM
Saddam Hussain
Saddam Hussain - avatar
4 odpowiedzi
+ 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);
17th Jul 2022, 3:45 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 can I use array?
17th Jul 2022, 3:48 PM
Saddam Hussain
Saddam Hussain - avatar
+ 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..!
17th Jul 2022, 3:57 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 thanks for your help 😘😘
18th Jul 2022, 1:42 AM
Saddam Hussain
Saddam Hussain - avatar