How to add 30 minutes to a JavaScript Date object?

前端 未结 20 2892
醉酒成梦
醉酒成梦 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:26
    var d1 = new Date (),
        d2 = new Date ( d1 );
    d2.setMinutes ( d1.getMinutes() + 30 );
    alert ( d2 );
    
    0 讨论(0)
  • 2020-11-22 00:26

    You should get the value of the current date to get the date with (ms) and add (30 * 60 *1000) to it. Now you have (current date + 30 min) with ms

    console.log('with ms', Date.now() + (30 * 60 * 1000))
    console.log('new Date', new Date(Date.now() + (30 * 60 * 1000)))

    0 讨论(0)
提交回复
热议问题