gpt4 book ai didi

jQuery 在日期中添加天数

转载 作者:行者123 更新时间:2023-12-01 06:39:00 25 4
gpt4 key购买 nike

我正在尝试向日期选择器输入添加天数以获取开始日期和结束日期。我得到的结束日期不正确,但不明白原因。

示例:选择的日期为 2011 年 10 月 25 日,第 1 天

结果: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;
}

最佳答案

我更喜欢将参数传递给函数而不是使用隐藏输入。我还希望日期以原始格式显示,因为我正在使用日期选择器,期望日期字符串采用特定格式。我还决定使该函数在日期分隔符和填充方面更加灵活。用法: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;
}

关于jQuery 在日期中添加天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7896378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com