Skip to content

Instantly share code, notes, and snippets.

@NishiBangar
Created July 26, 2021 05:28
Show Gist options
  • Select an option

  • Save NishiBangar/60409e361f64be6406e128331d9537ac to your computer and use it in GitHub Desktop.

Select an option

Save NishiBangar/60409e361f64be6406e128331d9537ac to your computer and use it in GitHub Desktop.
Subtract two dates - Angular - [15 days - 1 Month - 2 Months]
// Set time-period combo option
$scope.timePeriodOptions = [
{ option: "Last 15 days", model: "15Days" },
{ option: "Last 1 month", model: "1Month" },
{ option: "Last 2 months", model: "2Month" }
];
$scope.timePeriod= { startDate: "", endDate: "" };
if(timePeriod.model == "15Days" ){
$scope.startDate = new Date(new Date().setDate(new Date().getDate() -15 ));
$scope.endDate = new Date();
// Add preceding "0" to Date and Month is value < 10 -> To make a valid request to the server. (yyy-MM-DD)
$scope.timePeriod.startDate = $scope.startDate.getFullYear() +"-"+
(($scope.startDate.getMonth()+1) < 10 ? "0".concat( ($scope.startDate.getMonth()+1).toString() ) : ($scope.startDate.getMonth()+1)) +"-"+
($scope.startDate.getDate() < 10 ? "0".concat(($scope.startDate.getDate()).toString()) : $scope.startDate.getDate());
$scope.timePeriod.endDate = $scope.endDate.getFullYear() +"-"+
(($scope.endDate.getMonth()+1) < 10 ? "0".concat( ($scope.endDate.getMonth()+1).toString() ) : ($scope.endDate.getMonth()+1)) +"-"+
($scope.endDate.getDate() < 10 ? "0".concat(($scope.endDate.getDate()).toString()) : $scope.endDate.getDate());
}
else if(timePeriod.model == "1Month" ){
$scope.startDate = new Date(new Date().setMonth(new Date().getMonth() - 1 ));
$scope.endDate = new Date();
// Add preceding "0" to Date and Month is value < 10 -> To make a valid request to the server. (yyy-MM-DD)
$scope.timePeriod.startDate = $scope.startDate.getFullYear() +"-"+
(($scope.startDate.getMonth()+1) < 10 ? "0".concat( ($scope.startDate.getMonth()+1).toString() ) : ($scope.startDate.getMonth()+1)) +"-"+
($scope.startDate.getDate() < 10 ? "0".concat(($scope.startDate.getDate()).toString()) : $scope.startDate.getDate());
$scope.timePeriod.endDate = $scope.endDate.getFullYear() +"-"+
(($scope.endDate.getMonth()+1) < 10 ? "0".concat( ($scope.endDate.getMonth()+1).toString() ) : ($scope.endDate.getMonth()+1)) +"-"+
($scope.endDate.getDate() < 10 ? "0".concat(($scope.endDate.getDate()).toString()) : $scope.endDate.getDate());
}
else if(timePeriod.model == "2Month" ){
$scope.startDate = new Date(new Date().setMonth(new Date().getMonth() - 2 ));
$scope.endDate = new Date();
// Add preceding "0" to Date and Month is value < 10 -> To make a valid request to the server. (yyy-MM-DD)
$scope.timePeriod.startDate = $scope.startDate.getFullYear() +"-"+
(($scope.startDate.getMonth()+1) < 10 ? "0".concat( ($scope.startDate.getMonth()+1).toString() ) : ($scope.startDate.getMonth()+1)) +"-"+
($scope.startDate.getDate() < 10 ? "0".concat(($scope.startDate.getDate()).toString()) : $scope.startDate.getDate());
$scope.timePeriod.endDate = $scope.endDate.getFullYear() +"-"+
(($scope.endDate.getMonth()+1) < 10 ? "0".concat( ($scope.endDate.getMonth()+1).toString() ) : ($scope.endDate.getMonth()+1)) +"-"+
($scope.endDate.getDate() < 10 ? "0".concat(($scope.endDate.getDate()).toString()) : $scope.endDate.getDate());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment