Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将天数和月份添加到特定日期?_Jquery_Angularjs_Date_Add - Fatal编程技术网

Jquery 如何将天数和月份添加到特定日期?

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

我的日期是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 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个月。。