How to add 30 minutes to a JavaScript Date object?

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

    Use an existing library known to handle the quirks involved in dealing with time calculations. My current favorite is moment.js.

    <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
    <script>
     var now = moment(); // get "now"
     console.log(now.toDate()); // show original date
     var thirty = moment(now).add(30,"minutes"); // clone "now" object and add 30 minutes, taking into account weirdness like crossing DST boundries or leap-days, -minutes, -seconds.
     console.log(thirty.toDate()); // show new date
    </script>
    
    0 讨论(0)
  • 2020-11-22 00:10

    I know that the topic is way too old. But I am pretty sure that there are some developpers who still need this, so I made this simple script for you. I hope you enjoy it!

    Hello back, It's 2020 and I've added some modification hope it will help a lot better now!

    function strtotime(date, addTime){
      let generatedTime=date.getTime();
      if(addTime.seconds) generatedTime+=1000*addTime.seconds; //check for additional seconds 
      if(addTime.minutes) generatedTime+=1000*60*addTime.minutes;//check for additional minutes 
      if(addTime.hours) generatedTime+=1000*60*60*addTime.hours;//check for additional hours 
      return new Date(generatedTime);
    }
    
    Date.prototype.strtotime = function(addTime){
      return strtotime(new Date(), addTime); 
    }
    
    let futureDate = new Date().strtotime({
        hours: 16, //Adding one hour
        minutes: 45, //Adding fourty five minutes
        seconds: 0 //Adding 0 seconds return to not adding any second so  we can remove it.
    });
    <button onclick="console.log(futureDate)">Travel to the future</button>

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

    Here is my one-liner:

    console.log('time: ', new Date(new Date().valueOf() + 60000))

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

    Just another option, which I wrote:

    DP_DateExtensions Library

    It's overkill if this is all the date processing that you need, but it will do what you want.

    Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc. It's liberally open sourced.

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

    I always create 7 functions, to work with date in JS:
    addSeconds, addMinutes, addHours, addDays, addWeeks, addMonths, addYears.

    You can see an example here: http://jsfiddle.net/tiagoajacobi/YHA8x/

    How to use:

    var now = new Date();
    console.log(now.addMinutes(30));
    console.log(now.addWeeks(3));
    

    These are the functions:

    Date.prototype.addSeconds = function(seconds) {
      this.setSeconds(this.getSeconds() + seconds);
      return this;
    };
    
    Date.prototype.addMinutes = function(minutes) {
      this.setMinutes(this.getMinutes() + minutes);
      return this;
    };
    
    Date.prototype.addHours = function(hours) {
      this.setHours(this.getHours() + hours);
      return this;
    };
    
    Date.prototype.addDays = function(days) {
      this.setDate(this.getDate() + days);
      return this;
    };
    
    Date.prototype.addWeeks = function(weeks) {
      this.addDays(weeks*7);
      return this;
    };
    
    Date.prototype.addMonths = function (months) {
      var dt = this.getDate();
      this.setMonth(this.getMonth() + months);
      var currDt = this.getDate();
      if (dt !== currDt) {  
        this.addDays(-currDt);
      }
      return this;
    };
    
    Date.prototype.addYears = function(years) {
      var dt = this.getDate();
      this.setFullYear(this.getFullYear() + years);
      var currDt = this.getDate();
      if (dt !== currDt) {  
        this.addDays(-currDt);
      }
      return this;
    };
    
    0 讨论(0)
  • 2020-11-22 00:16

    Here is the IsoString version:

    console.log(new Date(new Date().setMinutes(new Date().getMinutes() - (30))).toISOString());

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