Ticker

6/recent/ticker-posts

Get Date And Time from system Using JavaScript...

<span>Photo by <a href="https://unsplash.com/@purzlbaum?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">🇨🇭 Claudio Schwarz | @purzlbaum</a> on <a href="https://unsplash.com/s/photos/javascript?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>



Here, I posted only the code because practical is much useful than the theory so the need for an explanation just uses the below code...

<html>
<head runat="server">
<title>jQuery display current time on webpage</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> 
</script>
<script type="text/javascript">
// datetime make using jquery
$(document)
.ready(function ()
{
ShowTime();
});

function ShowTime()
{
var dt = new Date();
document.getElementById("lblTime")
.innerHTML = dt.toLocaleTimeString();
window.setTimeout("ShowTime()", 1000); // Here 1000(milliseconds) means one 1 Sec
}
</script>
<script>
// date time make only javascript
function startTime()
{
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var d = new Date();
var n = d.getDate();
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 t = month[d.getMonth()];
var y = d.getFullYear();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt')
.innerHTML = n + "-" + t + "-" + y + " " + h + ":" + m + ":" + s;
var t = setTimeout(function ()
{
startTime()
}, 500);
}

function checkTime(i)
{
if(i < 10)
{
i = "0" + i
}; // add zero in front of numbers < 10
return i;
}
</script>
</head>
<body onload="startTime()">
<form id="form1">
<div> jQuery Display time second by second.
<label id="lblTime" style=" font-weight:bold"></label>
<br>
<br> Javascript Display time second by second. </div>
<div>
<div id="txt" style="color:#51545C; float:left; font-size:16px; font-weight:bold; margin-right: 12px;"></div>
</div>
</form>
</body>
</html> 

 
Copy and Paste the above code and run the program and you will get the Current System updating date and time...

Post a Comment

0 Comments