Steven Yong Steven Yong - 25 days ago 3
Javascript Question

setMinutes not gettng expected result

I have the following js codes:

// set a date time say 2 Oct
var localTime = new Date(2016, 9, 2, 4, 0, 0);

// set time to 23:59:59
localTime.setHours(23, 59, 59, 0);

console.log(localTime); // // Sun Oct 02 2016 23:59:59 GMT+0800 (MYT), which is expected

// now minus 600 minutes, which should be 10 hours
localTime.setMinutes(-600);
console.log(localTime); // Sun Oct 02 2016 13:00:59 GMT+0800 (MYT)


When I minus 600 minutes from that time, I am expecting it to minus 10 hours which should be 13:59:59 but it's printing 13:00:59

What is that I am missing here?

Answer

Date.prototype.setMinutes does not add/remove minutes from the time you have. It sets the minutes value for your date. The argument you provide is:

minutesValue

An integer between 0 and 59, representing the minutes.