Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JavaScriptQuestions javascript add minutes to time.

Have Programming Question? Ask it here!
 
 
 


ratna rathor
javascript add minutes to time.
1 Answer(s)      2 years and 3 months ago
Posted in : JavaScript Questions


I want to add minutes to time. How can I do this using javascript?


Advertisement
View Answers

May 14, 2012 at 5:21 PM


<html>
<head>
<title>Add minutes to current times</title>
<script type="text/javascript">
    function addMinutes() {
        var todayDate = new Date();
        var mins = document.getElementById("mins").value;
        var hours = todayDate.getHours();
        var minutes = todayDate.getMinutes();
        var newMins = parseInt(minutes) + parseInt(mins);
        if (newMins > 59) {
            var hrs = parseInt(newMins / 60);
            var min = newMins - hrs * 60;
        } else {
            min = newMins;
            hrs = 0;
        }
        var newHrs = parseInt(hours) + parseInt(hrs);

        var seconds = todayDate.getSeconds();
        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 + " : " + seconds + " "
                + format);
    }
</script>
</head>
<body>
<h2>Add minutes to current date and display..</h2>
Enter minutes :
<input type="text" name="mins" id="mins">
<input type="button" value="Add Minutes" onclick= "addMinutes();">
</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 minutes which you want to add into current time.parseInt() is used to convert the value in integer. Simple add minutes into the current minutes by writing var newMins = parseInt(minutes) + parseInt(mins);



Related Tutorials/Questions & Answers:
 

 

 

DMCA.com