Programming Examples
JavaScript script to display the current day and time in the following format
Write a JavaScript script to display the current day and time in the following format. Sample Output : Today is : Friday. Current time is : 4 PM : 50 : 22
Solution
<!DOCTYPE HTML>
<html>
<head>
<title>Current Time</title>
</head>
<body>
<script language="javascript">
var current_date = new Date();
var days =["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var mode=["AM","PM"];
var res;
res="Today is:"+days[current_date.getDay()]+". Current time is : "+(current_date.getHours())%12+" "+mode[parseInt(current_date.getHours()/12)]+" : "+current_date.getMinutes()+" : "+current_date.getSeconds();
document.write(res);
</script>
</body>
</html>
Output
Today is: Thursday. Current time is : 10 PM : 14 : 31