Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript add hours to time

I have this javascript code which should show the time. It works. I wan't to be able to add extra time though. Lets say that I want to add 1 hour.

        <script type="text/javascript">
        Date.prototype.addHours = function(h) {    
           this.setTime(this.getTime() + (h*60*60*1000)); 
           return this;   
        }
        // This function gets the current time and injects it into the DOM

        function updateClock() {
            // Gets the current time
            var now = new Date();

            // Get the hours, minutes and seconds from the current time
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();

            // Format hours, minutes and seconds
            if (hours < 10) {
                hours = "0" + hours;
            }
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (seconds < 10) {
                seconds = "0" + seconds;
            }

            // Gets the element we want to inject the clock into
            var elem = document.getElementById('clock');

            // Sets the elements inner HTML value to our clock data
            elem.innerHTML = hours + ':' + minutes + ':' + seconds;
        }
    function start(){
        setInterval('updateClock()', 200);
    }
    </script>

The first function calculates the milisecons that I want to add, and the second function is the "live clock". How do I implement the first function into the second one, so I get the working result?

like image 791
Niels Hermann Avatar asked Jul 13 '15 12:07

Niels Hermann


People also ask

How do I set hours in JavaScript?

setHours() The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance.

How do you add hours to a Date object?

To add 2 hours to the Date Object first, we get the current time by using the Date. getTime() method and then add 2 hour's milliseconds value (2 * 60 * 60 * 1000) to it and pass the added value to the Date Object.

How do you add hours in react JS?

To add hours to a Date in JavaScript:Use the setTime() method, passing it the result of calling getTime plus the hours converted to milliseconds.

How do you add minutes and hours in typescript?

Add hours using the setHours() Method. Add minutes using the setMinutes() Method. Add hours or minutes with the getTime() Method.


2 Answers

for adding hours, use setHours :

// Gets the current time
var now = new Date();

console.log("actual time:", now);

now.setHours(now.getHours() + 1)

console.log("actual time + 1 hour:", now);

For references: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

like image 133
ben Avatar answered Sep 24 '22 17:09

ben


Check out this fiddle.

The constructor Date(milliseconds) of class Date can be used here.

Here is the snippet.

var now = new Date();
alert(now);

var milliseconds = new Date().getTime() + (1 * 60 * 60 * 1000);
var later = new Date(milliseconds);
alert(later);
like image 30
Shrinivas Shukla Avatar answered Sep 23 '22 17:09

Shrinivas Shukla