Website Scripts And Cloud Tutorials

How to display date time in javascript

Byadmin

Jan 5, 2010 ,

Last updated on February 2nd, 2022 at 05:02 pm

Click to rate this tutorial!
[Total: 2 Average: 5]

Javascript Clock with Date Time

This is a simple tutorial on how to create a clock with date and time for your website using JavaScript. Add the code below to the section of your website where you want the clock to be displayed.

If you just need to display Time, Go to this script.

<span id="clock"></span>
<script type="text/javascript">
function startclock()//script from mistonline.in
{
  var thetime=new Date();
  var nhours=thetime.getHours();
  var nmins=thetime.getMinutes();
  var nsecn=thetime.getSeconds();
  var nday=thetime.getDay();
  var nmonth=thetime.getMonth();
  var ntoday=thetime.getDate();
  var nyear=thetime.getYear();
  var AorP = (nhours >= 12) ? "P.M." : "A.M.";
  if (nhours>=13)
    nhours-=12;
  if (nhours<1)
   nhours=12;
  if (nsecn<10)
   nsecn="0"+nsecn;
  if (nmins<10)
   nmins="0"+nmins;
  switch (nday) {
    case 0 : nday = "Sunday"; break;
    case 1 : nday = "Monday"; break;
    case 2 : nday = "Tuesday"; break;
    case 3 : nday = "Wednesday"; break;
    case 4 : nday = "Thursday"; break;
    case 5 : nday = "Friday"; break;
    case 6 : nday = "Saturday"; break;
  }
  nmonth+=1;
  if (nyear<=99)
    nyear= "19"+nyear;
  if ((nyear>99) && (nyear<2000))
   nyear+=1900;

  var clock_span = document.getElementById("clock");
  clock_span.innerHTML = nhours+":"+nmins+":"+nsecn+" "+AorP+" "+nday+", "+nmonth+"/"+ntoday+"/"+nyear;

setTimeout('startclock()',1000);
}

if (document.getElementById && document.createTextNode) {
  startclock();
}
</script>

Demo

Thanks 🙂

Click to rate this tutorial!
[Total: 2 Average: 5]

Leave a Reply

Your email address will not be published.