jQuery 将日期添加到日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7896378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:24:50  来源:igfitidea点击:

jQuery adding days to a date

jquery

提问by Keith Power

I am trying to add a number of days to a date selector input to get a start date and end date. I am getting an incorrect end date and cant see why.

我正在尝试向日期选择器输入添加天数以获取开始日期和结束日期。我得到的结束日期不正确,不知道为什么。

Example: Date selected 25-10-2011, days 1

示例:选择日期 25-10-2011,第 1 天

Result: date_start 2011-9-25, date_end 2012-5-7

结果:date_start 2011-9-25,date_end 2012-5-7

 function makeUpDates(){
      // concantenate values to date_start and date_end hidden inputs
      var dateString = document.getElementById('date').value,
      date = new Date(dateString);
      document.getElementById('date_start').value = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();

      var numDays = document.getElementById('slider').value;
      date.setDate(date.getDate() + numDays);   
      var dateEnd = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
      document.getElementById('date_end').value = dateEnd;
 }

回答by AWizardInDallas

I prefer passing arguments to functions rather than using hidden inputs. I also wanted the date to come out in the original format, since I'm working with date pickers expecting the date string in a particular format. I also decided to make the function a little more flexible with regard to the date separator and padding. Usage: AddDaysToDate("03/18/2016", 5, "/"), adds 5 days so returns "03/23/2016."

我更喜欢将参数传递给函数而不是使用隐藏的输入。我还希望日期以原始格式出现,因为我正在使用期望日期字符串为特定格式的日期选择器。我还决定使函数在日期分隔符和填充方面更加灵活。用法:AddDaysToDate("03/18/2016", 5, "/"),加上 5 天所以返回 "03/23/2016"。

function AddDaysToDate(sDate, iAddDays, sSeperator) {
    //Purpose: Add the specified number of dates to a given date.
    var date = new Date(sDate);
    date.setDate(date.getDate() + parseInt(iAddDays));
    var sEndDate = LPad(date.getMonth() + 1, 2) + sSeperator + LPad(date.getDate(), 2) + sSeperator + date.getFullYear();
    return sEndDate;
}
function LPad(sValue, iPadBy) {
    sValue = sValue.toString();
    return sValue.length < iPadBy ? LPad("0" + sValue, iPadBy) : sValue;
}

回答by Royi Namir

  var today       =new Date('12/22/1978');
 var in_a_day   =new Date(today).setDate(today.getDate()+1); //+1 or +x

edit your date format should be : mm/dd/yyyy

编辑您的日期格式应该是: mm/dd/yyyy

回答by Keith Power

The issue was that the numDays had to be converted to an integer before added. Otherwise I was getting strange results.

问题是在添加之前必须将 numDays 转换为整数。否则我会得到奇怪的结果。

function makeUpDates(){
      // concantenate values to date_start and date_end hidden inputs
      var dateString = document.getElementById('date').value,
      date = new Date(dateString);
      alert (date);
      document.getElementById('date_start').value = date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate();

      var numDays = document.getElementById('slider').value;
      date.setDate(date.getDate() + parseInt(numDays)); 

      var dateEnd = date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate();
      document.getElementById('date_end').value = dateEnd;
 }