HI WELCOME TO SIRIS

JavaScript Date and Time Function

JavaScript is use to set the current date/time in different different format (like milliSecond, minutes, hours etc).

Display Today Date

Example
<!DOCTYPE html>
<html lang="en">
<head>
  <title>JavaScript today date</title>
</head>
<body>
  <script type="text/javascript"> 
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth()
    var year = currentDate.getFullYear()
    document.write("Current Date: " + day + "-" + month + "-" + year);
  </script>
</body>
</html>

Example Result

Display Current Time (24 Hours)

Example
<!DOCTYPE html>
<html lang="en">
<head>
  <title>JavaScript current time</title>
</head>
<body>
  <script type="text/javascript"> 
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10) { 
       minutes = "0" + minutes;
    }
    document.write(""); 
    document.write("Current Time: " + hours + ":" + minutes);
  </script>
</body>
</html>

Example Result

Display Current Time (AM/PM format)

Example
<!DOCTYPE html>
<html lang="en">
<head>
  <title>JavaScript current time (AM/PM)</title>
</head>
<body>
  <script type="text/javascript"> 
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
   
    var am_pm = "AM";
    if (hours >= 12) {
      am_pm = "PM";
      hours = hours - 12;
    }
    if (hours == 0) {
      hours = 12;
    }
    if (minutes < 10) {
      minutes = "0" + minutes;
    }
    document.write("Current Time: ");  
    document.write("Current Time: " + hours + ":" + minutes + " " + am_pm);
  </script>
</body>
</html>

Example Result