Adding hours to JavaScript Date object?

前端 未结 16 1196
一生所求
一生所求 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

提交回复
热议问题