Adding hours to JavaScript Date object?

前端 未结 16 1164
一生所求
一生所求 2020-11-22 09:00

It amazes me that JavaScript\'s Date object does not implement an add function of any kind.

I simply want a function that can do this:

var now = Date         


        
相关标签:
16条回答
  • 2020-11-22 09:35

    A little messy, but it works!

    Given a date format like this: 2019-04-03T15:58

      //Get the start date.
      var start = $("#start_date").val();
      //Split the date and time.
      var startarray = start.split("T");
      var date = startarray[0];
      var time = startarray[1];
    
      //Split the hours and minutes.
      var timearray = time.split(":");
    
      var hour = timearray[0];
      var minute = timearray[1];
      //Add an hour to the hour.
      hour++;
      //$("#end_date").val = start;
      $("#end_date").val(""+date+"T"+hour+":"+minute+"");
    

    Your output would be: 2019-04-03T16:58

    0 讨论(0)
  • 2020-11-22 09:38

    You can use the momentjs http://momentjs.com/ Library.

    var moment = require('moment');
    foo = new moment(something).add(10, 'm').toDate();
    
    0 讨论(0)
  • 2020-11-22 09:40

    This is a easy way to get incremented or decremented data value.

    const date = new Date()
    const inc = 1000 * 60 * 60 // an hour
    const dec = (1000 * 60 * 60) * -1 // an hour
    
    const _date = new Date(date)
    return new Date( _date.getTime() + inc )
    return new Date( _date.getTime() + dec )
    
    0 讨论(0)
  • 2020-11-22 09:44

    Another way to handle this is to convert the date to unixtime (epoch), then add the equivalent in (milli)seconds, then convert it back. This way you can handle day and month transitions, like adding 4 hours to 21, which should result in the next day, 01:00.

    0 讨论(0)
  • 2020-11-22 09:45

    There is an add in the Datejs library.

    And here are the JavaScript date methods. kennebec wisely mentioned getHours() and setHours();

    0 讨论(0)
  • 2020-11-22 09:49

    SPRBRN is correct. In order to account for the beginning/end of the month and year, you need to convert to Epoch and back.

    Here's how you do that:

    var milliseconds = 0;          //amount of time from current date/time
    var sec = 0;                   //(+): future
    var min = 0;                   //(-): past
    var hours = 2;
    var days = 0;
    
    var startDate = new Date();     //start date in local time (we'll use current time as an example)
    
    var time = startDate.getTime(); //convert to milliseconds since epoch
    
    //add time difference
    var newTime = time + milliseconds + (1000*sec) + (1000*60*min) + (1000*60*60*hrs) + (1000*60*60*24*days);
    
    var newDate = new Date(newTime); //convert back to date; in this example: 2 hours from right now
    

    or do it in one line (where variable names are the same as above:
    var newDate = 
        new Date(startDate.getTime() + millisecond + 
            1000 * (sec + 60 * (min + 60 * (hours + 24 * days))));
    
    0 讨论(0)
提交回复
热议问题