我想计算两个日期之间的确切月份和日期。如何计算两个日期之间的时间段 - angularjs
如果我的开始日期是“2014年1月12日”,我的结束日期是“2017年3月27日”。
我应该得到“38个月和15天”。
但我只能找到没有。从开始日期到结束日期之间的天数。我需要一些帮助来查找开始日期和结束日期之间的月份和日期。
然后我需要将15天分为no。结束日期月份的天数。
任何人都可以帮助我吗?我是新的日期功能。
var date = new Date();
console.log("date: "+date);
var currentDate = $filter('date')(date, "yyyy-MM-dd HH:mm:ss");
$scope.userdob = "2017-01-29";
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]);
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
var date1_unixtime = parseInt(date1.getTime()/1000);
var date2_unixtime = parseInt(date2.getTime()/1000);
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
// in Hours
var timeDifferenceInHours = timeDifference/60/60;
// and finaly, in days :)
$scope.timeDifferenceInDays = timeDifferenceInHours/24;
console.log("timeDifferenceInDays: "+$scope.timeDifferenceInDays);
为什么不使用像Moment.JS这样的现有库? – jonrsharpe
不像秒,分和小时;月份和日期没有固定的比例。一个月可以是从“28”到“31”天的任何事情。当你要在_months_中测量事物时,也许你不需要包括这些日子。 – Halcyon