This example will help a user to get the current time,date, day, month and the year.
Here we have used JavaScript Date class and the following methods of Date class.
- getFullYear(); getFullYear() returns a four-digit number
- getMonth(); The value returned by getMonth() is an integer between 0 and 11
- getDay(); The value returned by getDay() is an integer corresponding to the day of the week
- getHours(); The value returned by getHours() is an integer between 0 and 23.
- getMinutes(); The value returned by getMinutes() is an integer between 0 and 59.
- getSeconds(); The value returned by getSeconds() is an integer between 0 and 59.
- getDate(); represents the day of the month.
Now create an htmlpage index.php
<html>
<head>
<title>Get Current time</title>
<script src="datemonth.js" type="text/javascript">
</head>
<body>
<div>
<div style="overflow: hidden; height: 55%; ">
<strong><u>Current date & time :</u></strong><span> </span><span id="txt"></span>
<p style=" float:left; margin-left:10px; padding-top:50px;"></p>
</div>
</div>
</body>
</html>
Now create JavaScript file datemonth.js
var now = new Date();
var Y = now.getFullYear();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var M = month[now.getMonth()];
var weekday = new Array(7);
weekday[0]= "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var D = weekday[now.getDay()];
var H = now.getHours();
var Mnt = now.getMinutes();
var S = now.getSeconds();
var date = now.getDate();
var time = document.getElementById("txt").innerHTML=date+'-'+M+'-'+Y+' '+D+' '+H+':'+Mnt+':'+S;
0 Comment(s)