
<html>
<head>
<title>Add seconds to current times</title>
<script type="text/javascript">
function addSeconds() {
var todayDate = new Date();
var secs = document.getElementById("sec").value;
var hours = todayDate.getHours();
var minutes = todayDate.getMinutes();
var seconds = todayDate.getSeconds();
var newSec = parseInt(seconds) + parseInt(secs);
if (newSec > 59) {
var mins = parseInt(newSec / 60);
var sec = newSec - mins * 60;
var newMin = parseInt(minutes) + mins;
if (newMin > 59) {
var hrs = parseInt(newMin / 60);
var min = newMin - (hrs * 60);
var newHrs = parseInt(hours) + hrs;
} else {
newHrs = hours;
min = newMin;
}
} else {
newHrs = hours;
min = minutes;
sec = newSec;
}
var format = "AM";
if (newHrs > 11) {
format = "PM";
}
if (newHrs > 12) {
newHrs = newHrs - 12;
}
if (newHrs == 0) {
newHrs = 12;
}
if (min < 10) {
min = "0" + min;
}
document.write("Time : " + newHrs + " : " + min + " : " + sec + " "
+ format);
}
</script>
</head>
<body>
<h2>Add Seconds to current date and display..</h2>
Enter Seconds :
<input type="text" name="sec" id="sec">
<input type="button" value="Add Seconds" onclick="addSeconds()";>
</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 seconds which you want to add into current time.parseInt() is used to convert the value in integer.Simple add seconds into the current second by writing var newSec = parseInt(seconds) + parseInt(secs);you will get new second.Now you must check , seconds will not exceed to 59.If it is more than 59 then convert seconds into minutes and add it into current minutes and same procedure for hours.
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.