Convert Time : Date Display « Date « JavaScript Tutorial






<html>
<head>
<title>Convert Time</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--

var current_date = new Date();
var hour_value = current_date.getHours();
var minute_value = current_date.getMinutes();
var second_value = current_date.getSeconds();
var AMorPM = "AM";

if (hour_value > 12)
{
  hour_value -= 12;
  AMorPM = "PM";
}

if (hour_value == 0)
   {
   hour_value = 12;
   AMorPM = "AM";
}

if (second_value < 10)
{
  second_value = "0" + second_value;
}

if (minute_value < 10)
{
  minute_value = "0" + minute_value;
}

document.write("The current time is " + hour_value + ":" +
minute_value + ":" + second_value + " " + AMorPM);

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








12.4.Date Display
12.4.1.Convert Time