ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

Date and Time JavaScript


<!DOCTYPE html>
<html>
<head>
<title>Date Examples</title>
<style>
p span{font-weight:bold; color:red;}
</style>
</head>
<body>

<p>Year: <span id="demo1"></span></p>
<p>Month: <span id="demo2"></span></p>
<p>Day: <span id="demo3"></span></p>
<p>Hour: <span id="demo4"></span></p>
<p>Minutes: <span id="demo5"></span></p>
<p>Seconds: <span id="demo6"></span></p>

<script>
document.getElementById("demo1").innerHTML = new Date().getFullYear();
document.getElementById("demo2").innerHTML = (new Date().getMonth()+1);
document.getElementById("demo3").innerHTML = new Date().getDate();
document.getElementById("demo4").innerHTML = new Date().getHours();
document.getElementById("demo5").innerHTML = new Date().getMinutes();
document.getElementById("demo6").innerHTML = new Date().getSeconds();
</script>

 

<hr>

<p>Month Name: <span id="demo7"></span></p>
<p>Day Name: <span id="demo8"></span></p>

 
<script>
// Month with month name ,, with switch case
var monthNumber = new Date().getMonth();
var month;
switch (monthNumber) {
case 0:
month = "January";
break;
case 1:
month = "February";
break;
case 2:
month = "March";
break;
case 3:
month = "April";
break;
case 4:
month = "May";
break;
case 5:
month = "June";
break;
case 6:
month = "July";
break;
case 7:
month = "August";
break;
case 8:
month = "September";
break;
case 9:
month = "October";
break;
case 10:
month = "November";
break;
case 11:
month = "December";

}
document.getElementById("demo7").innerHTML = month;

// day with day name,, with switch case
var day;
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.getElementById("demo8").innerHTML = day;

</script>
 
</body>
</html>

Categroy:
Front-end , javascripts