How to add 30 minutes to a JavaScript Date object?

前端 未结 20 2961
醉酒成梦
醉酒成梦 2020-11-21 23:21

I\'d like to get a Date object which is 30 minutes later than another Date object. How do I do it with JavaScript?

20条回答
  •  旧巷少年郎
    2020-11-22 00:22

    This is what I do which seems to work quite well:

    Date.prototype.addMinutes = function(minutes) {
        var copiedDate = new Date(this.getTime());
        return new Date(copiedDate.getTime() + minutes * 60000);
    }
    

    Then you can just call this like this:

    var now = new Date();
    console.log(now.addMinutes(50));
    

提交回复
热议问题