Jquery 如何将天数和月份添加到特定日期?
我的日期是2017年5月31日。有了这个日期,我需要再增加两天。 然后是另一个要求,同一天我需要增加18个月。 我尝试了以下代码Jquery 如何将天数和月份添加到特定日期?,jquery,angularjs,date,add,Jquery,Angularjs,Date,Add,我的日期是2017年5月31日。有了这个日期,我需要再增加两天。 然后是另一个要求,同一天我需要增加18个月。 我尝试了以下代码 var rootDate = 31 May,2017; var firstDate = new Date(); var lastDate = new Date(); var tempdate = firstDate.setDate(rootDate.getDate() +2); firstDate = $filter('date')(tempdate , "dd
var rootDate = 31 May,2017;
var firstDate = new Date();
var lastDate = new Date();
var tempdate = firstDate.setDate(rootDate.getDate() +2);
firstDate = $filter('date')(tempdate , "dd MMM, yyyy");
var tempDateTwo = lastDate.setDate(rootDate.getMonth() +18);
secondDate = $filter('date')(tempDateTwo , "dd MMM, yyyy");
它给了我一个错误“无效日期”。代码中有什么错误?使用此1:用于添加天
moment()。添加(2,'days')。格式('DD-MMMM,YYYY')代码>
2:用于添加月份moment()。添加(18,'months')。格式('DD-MMMM,YYYY')代码>使用此
1:用于添加天moment()。添加(2,'days')。格式('DD-MMMM,YYYY')代码>
2:用于添加月份moment()。添加(18,'months')。格式('DD-MMMM,YYYY')编码>首先,将日期转换为日期对象
var rootDate=新日期(“2017年5月31日”)代码>
要设置月份,请使用setMonth
方法
lastDate.setMonth(rootDate.getMonth()+18)代码>
角度模块(“应用程序”,[])
.controller(“ctrl”,函数($scope,$filter){
var rootDate=新日期(“2017年5月31日”);
var firstDate=新日期();
var lastDate=新日期();
var tempdate=firstDate.setDate(rootDate.getDate()+2);
firstDate=$filter('date')(tempdate,“dd-MMM,yyyy”);
console.log(firstDate)
var tempDateTwo=lastDate.setMonth(rootDate.getMonth()+18);
lastDate=$filter('date')(tempDateTwo,“dd-MMM,yyyy”);
console.log(lastDate)
})
首先,将日期转换为日期对象
var rootDate=新日期(“2017年5月31日”)代码>
要设置月份,请使用setMonth
方法
lastDate.setMonth(rootDate.getMonth()+18)代码>
角度模块(“应用程序”,[])
.controller(“ctrl”,函数($scope,$filter){
var rootDate=新日期(“2017年5月31日”);
var firstDate=新日期();
var lastDate=新日期();
var tempdate=firstDate.setDate(rootDate.getDate()+2);
firstDate=$filter('date')(tempdate,“dd-MMM,yyyy”);
console.log(firstDate)
var tempDateTwo=lastDate.setMonth(rootDate.getMonth()+18);
lastDate=$filter('date')(tempDateTwo,“dd-MMM,yyyy”);
console.log(lastDate)
})
我已经修改了您的代码,以这种方式工作,#将带来您想要实现的目标
var rootDate = '31 May,2017';
var firstDate = new Date(rootDate);
var lastDate = new Date(rootDate);
firstDate.setDate(firstDate.getDate() + parseInt(2));
lastDate.setMonth(lastDate.getMonth() + parseInt(18));
我已经修改了您的代码,以这种方式工作,#将带来您想要实现的目标
var rootDate = '31 May,2017';
var firstDate = new Date(rootDate);
var lastDate = new Date(rootDate);
firstDate.setDate(firstDate.getDate() + parseInt(2));
lastDate.setMonth(lastDate.getMonth() + parseInt(18));
更改var-rootDate=新日期(“2017年5月31日”)
如果你想使用日期,我建议你看看@sachila谢谢..它可以增加2天,而不是增加18个月..更改var-rootDate=新日期(“2017年5月31日”)
如果你想使用日期,我建议你看看@sachila,谢谢你。它只适用于增加2天,不适用于增加18个月。。