7

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?

2

4 Answers 4

9

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

6

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);

0
0

Check out this
fiddle here

var todayDate = new Date();
alert("After adding ONE hour : "+new Date(todayDate.setHours(todayDate.getHours()+1)) );

1
  • While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – secelite
    Commented Nov 9, 2016 at 9:25
0

javascript date API is near to be completed, the existing methods of it can be use to add another functionality for this API, some says it is tedious but its not.

in order to add a method in a date we will access the prototype of this API,

like this.

Date.prototype.addTime = function(str){
    function parse(str){
        let arr = (typeof str == 'number')?[str]:str.split(":").map(t=>t.trim());
        arr[0] = arr[0] || 0;
        arr[1] = arr[1] || 0;
        arr[2] = arr[2] || 0;
        return arr
    }
    function arrToMill(arr){
        let [h,m,s] = arr;
        return (h*60*60*1000) + (m*60*1000) + (s*1000); 
    }
    let date = new Date(this.getTime());
    let parsed = parse(str);
    date.setTime(date.getTime() + arrToMill(parsed));
    return date;
}

getting it rockin. this function is immutable

let date = new Date();
date.addTime(1);
date.addTime("01:00");`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.