How to add 30 minutes to a JavaScript Date object?

前端 未结 20 2987
醉酒成梦
醉酒成梦 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:01

    For the lazy like myself:

    Kip's answer (from above) in coffeescript, using an "enum", and operating on the same object:

    Date.UNIT =
      YEAR: 0
      QUARTER: 1
      MONTH: 2
      WEEK: 3
      DAY: 4
      HOUR: 5
      MINUTE: 6
      SECOND: 7
    Date::add = (unit, quantity) ->
      switch unit
        when Date.UNIT.YEAR then @setFullYear(@getFullYear() + quantity)
        when Date.UNIT.QUARTER then @setMonth(@getMonth() + (3 * quantity))
        when Date.UNIT.MONTH then @setMonth(@getMonth() + quantity)
        when Date.UNIT.WEEK then @setDate(@getDate() + (7 * quantity))
        when Date.UNIT.DAY then @setDate(@getDate() + quantity)
        when Date.UNIT.HOUR then @setTime(@getTime() + (3600000 * quantity))
        when Date.UNIT.MINUTE then @setTime(@getTime() + (60000 * quantity))
        when Date.UNIT.SECOND then @setTime(@getTime() + (1000 * quantity))
        else throw new Error "Unrecognized unit provided"
      @ # for chaining
    
    0 讨论(0)
  • 2020-11-22 00:03

    Here is the ES6 version:

    let getTimeAfter30Mins = () => {
      let timeAfter30Mins = new Date();
      timeAfter30Mins = new Date(timeAfter30Mins.setMinutes(timeAfter30Mins.getMinutes() + 30));
    };
    

    Call it like:

    getTimeAfter30Mins();
    
    0 讨论(0)
  • 2020-11-22 00:06

    var now = new Date();
    now.setMinutes(now.getMinutes() + 30); // timestamp
    now = new Date(now); // Date object
    console.log(now);

    0 讨论(0)
  • 2020-11-22 00:07

    Other solution:

    var dateAv = new Date();
    var endTime = new Date(dateAv.getFullYear(), dateAv.getMonth(), dateAv.getDate(), dateAv.getHours(), dateAv.getMinutes() + 30);
              
    
    0 讨论(0)
  • 2020-11-22 00:08

    The easiest way to solve is the to recognize that in javascript dates are just numbers. It starts 0 or 'Wed Dec 31 1969 18:00:00 GMT-0600 (CST). Every 1 represents a millisecond. You can add or subtract milliseconds by getting the value and instantiating a new date using that value. You can manage it pretty easy with that mind.

    const minutesToAdjust = 10;
    const millisecondsPerMinute = 60000;
    const originalDate = new Date('11/20/2017 10:00 AM');
    const modifiedDate1 = new Date(originalDate.valueOf() - (minutesToAdjust * millisecondsPerMinute));
    const modifiedDate2 = new Date(originalDate.valueOf() + (minutesToAdjust * millisecondsPerMinute));
    
    console.log(originalDate); // Mon Nov 20 2017 10:00:00 GMT-0600 (CST)
    console.log(modifiedDate1); // Mon Nov 20 2017 09:50:00 GMT-0600 (CST)
    console.log(modifiedDate2); // Mon Nov 20 2017 10:10:00 GMT-0600 (CST)
    
    0 讨论(0)
  • 2020-11-22 00:10

    You could do this:

    let thirtyMinutes = 30 * 60 * 1000; // convert 30 minutes to milliseconds
    let date1 = new Date();
    let date2 = new Date(date1.getTime() + thirtyMinutes);
    console.log(date1);
    console.log(date2);

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