如何将天数和月份添加到特定日期?
How to add no of days as well as month to specific date?
我有一个日期,例如 2017 年 5 月 31 日。有了这个日期,我需要再添加 2 天。
然后另一个要求,对于同一日期我需要添加 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");
它给我错误 'Invalid Date'。代码有什么问题?
使用这个
1:For 添加天数 moment().add(2,'days').format('DD MMMM, YYYY');
。
2:For 添加月份 moment().add(18,'months').format('DD MMMM, YYYY');;
首先,将日期转换为日期对象
var rootDate = new Date("31 May,2017");
并设置月份使用 setMonth
方法
lastDate.setMonth(rootDate.getMonth() +18);
angular.module("app",[])
.controller("ctrl",function($scope,$filter){
var rootDate = new Date("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");
console.log(firstDate)
var tempDateTwo = lastDate.setMonth(rootDate.getMonth() +18);
lastDate = $filter('date')(tempDateTwo , "dd MMM, yyyy");
console.log(lastDate)
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
</div>
我已经修改了你的代码以这样工作,# 将带来你想要实现的目标。
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));
我有一个日期,例如 2017 年 5 月 31 日。有了这个日期,我需要再添加 2 天。 然后另一个要求,对于同一日期我需要添加 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");
它给我错误 'Invalid Date'。代码有什么问题?
使用这个
1:For 添加天数 moment().add(2,'days').format('DD MMMM, YYYY');
。
2:For 添加月份 moment().add(18,'months').format('DD MMMM, YYYY');;
首先,将日期转换为日期对象
var rootDate = new Date("31 May,2017");
并设置月份使用 setMonth
方法
lastDate.setMonth(rootDate.getMonth() +18);
angular.module("app",[])
.controller("ctrl",function($scope,$filter){
var rootDate = new Date("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");
console.log(firstDate)
var tempDateTwo = lastDate.setMonth(rootDate.getMonth() +18);
lastDate = $filter('date')(tempDateTwo , "dd MMM, yyyy");
console.log(lastDate)
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
</div>
我已经修改了你的代码以这样工作,# 将带来你想要实现的目标。
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));