
I want to add numbers of hours to time. How can I do this using javascript?

<html>
<head>
<title>Add hours to current times</title>
<script type="text/javascript">
function addHours() {
var todayDate = new Date();
var hrs = document.getElementById("hrs").value;
var hours = todayDate.getHours();
var newHrs = parseInt(hours) + parseInt(hrs);
var minutes = todayDate.getMinutes();
var seconds = todayDate.getSeconds();
var format = "AM";
if (newHrs > 11) {
format = "PM";
alert(newHrs);
}
if (newHrs > 12) {
newHrs = newHrs - 12;
}
if (newHrs == 0) {
newHrs = 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
document.write("Time : " + newHrs + " : " + minutes + " : " + seconds
+ " " + format);
}
</script>
</head>
<body>
<h2>Add hours to current date and display..</h2>
Enter hours :
<input type="text" name="hrs" id="hrs">
<input type="button" value="Add Hours" onclick="addHours();">
</body>
</html>
Description: In javascript whenever you write new Date() it shows the current date in standard format with current day,date,time and GMT.You can have your own format.
getHours() shows hours of specified date.
getMinutes() shows minutes of specified date.
getSeconds() shows seconds of specified date.
Input hours which you want to add into current time.parseInt() is used to convert the value in integer.Simple add hours into the current hour by writing var newHrs = parseInt(hours) + parseInt(hrs);
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.