Javascript Current Date and Time
Current Date and Time using Javascript
This Code will output the Current Date (MM/DD/YYYY) and Time (12 hour method) using Javascript
Javascript
<!DOCTYPE html>
<title>Date and Time</title>
<time id="date"></time>
<time id="time"></time>
<script>
var curDate = new Date(),
month = curDate.getMonth() + 1,
day = curDate.getDate(),
year = curDate.getFullYear(),
date = month + "/" + day + "/" + year;
document.getElementById("date").innerHTML = date;
var curTime = new Date(),
hours = curTime.getHours(),
minutes = curTime.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;}
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;}
if (hours == 0) {
hours = 12;}
time = hours + ":" + minutes + " " + suffix;
document.getElementById("time").innerHTML = time;
</script>
Click here to Run this Code
Share with Friends









Comments
Post a Comment