我想计算两个日期之间的月份和天数。然后需要计算用户在开始日期和结束日期之间支付的费用。
费用= 10000.00; 周期性= 3(Quaterly);
月份和天数=(开始日期 - 结束日期);
付款需要支付=((月数和日期/周期数)*费用);
我试着找到两个日期之间的时差,但它对我来说不能正常工作。任何人都可以帮助我吗?
var date = "2017-06-01";
console.log("date: "+date);
var currentDate = $filter('date')(date, "yyyy-MM-dd HH:mm:ss");
$scope.userdob = "2017-08-31";
var dobdate = $filter('date')($scope.userdob, "yyyy-MM-dd HH:mm:ss");
console.log("dob: "+dobdate);
/* differentiate Date */
var date1 = $filter('date')($scope.userdob, "yyyy-MM-dd");
var date2 = $filter('date')(date, "yyyy-MM-dd");
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[0], date1[1], date1[2]);
date2 = new Date(date2[0], date2[1], date2[2]);
if (date1 < date2)
{
var start_date = date1;
var end_date = date2;
var inverse = false;
end_date = new Date(end_date); //If you don't do this, the original date passed will be changed. Dates are mutable objects.
end_date.setDate(end_date.getDate() + 1);
// Calculate the differences between the start and end dates
var yearsDifference = end_date.getFullYear() - start_date.getFullYear();
var monthsDifference = end_date.getMonth() - start_date.getMonth();
var daysDifference = end_date.getDate() - start_date.getDate();
var d1 = new Date(end_date.getFullYear(), end_date.getMonth(), 0);
var noOfDays = d1.getDate();
$scope.noOfMonths = (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + daysDifference/noOfDays); // Add fractional month
答案 0 :(得分:1)
function findDateDifferenceInDays(sdate,edate) {
var oneDay = 24*60*60*1000;
var firstDate = new Date(sdate);
var secondDate = new Date(edate);
return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
}
答案 1 :(得分:0)
为了做到正确,日期操纵是非常棘手的。有闰年,闰秒,以及其他各种形式的时间调整。对于一个很大但不太详尽的列表,请结帐Falsehoods programmers believe about time和More falsehoods programmers believe about time。在做日期和时间非常重要的事情时,最好的选择是使用一个好的库,例如moment。当然,它并不能使一切正常,但它在大多数情况下都能做得足够好。
它也可以为你处理解析,所以前面的代码可以像这样:
public void maxFromName(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COLUMN_MAX + " FROM "+ TABLE_AZKAR+" WHERE " +COLUMN_NAME+ "=" + name ;
db.execSQL(query);
}
答案 2 :(得分:0)
这解决了我的问题。
$scope.startDate = new Date(startDate);
$scope.endDate = new Date(endDate);
console.log($scope.startDate);
console.log($scope.endDate);
/* differentiate Date */
var date1 = $filter('date')($scope.startDate, "yyyy-MM-dd");
var date2 = $filter('date')($scope.endDate, "yyyy-MM-dd");
console.log("date1 : "+date1);
console.log("date2 : "+date2);
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
/*date1 = new Date(date1[0], date1[1], date1[2]);
date2 = new Date(date2[0], date2[1], date2[2]);*/
console.log("date1 : "+date1);
console.log("date2 : "+date2);
if ($scope.startDate < $scope.endDate)
{
var start_date = date1;
var end_date = date2;
var inverse = false;
console.log("end_date : "+end_date);
end_date = new Date(end_date); //If you don't do this, the original date passed will be changed. Dates are mutable objects.
console.log("end_date : "+end_date); end_date.setDate(end_date.getDate() + 1);
start_date = new Date(start_date);
// Calculate the differences between the start and end dates
var yearsDifference = end_date.getFullYear() - start_date.getFullYear();
var monthsDifference = end_date.getMonth() - start_date.getMonth();
var daysDifference = end_date.getDate() - start_date.getDate();
var d1 = new Date(end_date.getFullYear(), end_date.getMonth(), 0);
var noOfDays = d1.getDate();
if(noOfDays >= 30) {
noOfDays = 30;
}
var days = daysDifference/noOfDays;
console.log(days);
$scope.noOfMonths = (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + days); // Add fractional month
console.log($scope.noOfMonths);