var diffTraker = new DiffTracker(new Date(2017, 7, 23, 9), new Date(2017, 7, 24, 9));
console.log('Start Date');
console.log(new Date(2017, 7, 23, 9));
console.log('-------------------');
console.log('End Date');
console.log(new Date(2017, 7, 24, 9));
console.log('-------------------');
console.log(diffTraker.getPercentage(new Date(2017, 7, 23, 21)) + '% from start date (' + new Date(2017, 7, 23, 21) + ')');
console.log(diffTraker.getPercentage(new Date(2017, 7, 24, 3)) + '% from start date (' + new Date(2017, 7, 24, 3) + ')');
console.log(diffTraker.getPercentage(new Date(2017, 7, 24, 5)) + '% from start date (' + new Date(2017, 7, 24, 5) + ')');
function DiffTracker(startDate, endDate){
var self = this;
self.start = startDate;
self.end = endDate;
self.totalHours = getDiffHours(self.start, self.end);
self.getPercentage = function(date){
var hoursFromStart = getDiffHours(self.start, date);
return (hoursFromStart * 100/self.totalHours).toFixed(2);
}
function getDiffHours(start, end){
/* 36e5 is the scientific notation for 60*60*1000, dividing by which converts the milliseconds difference into hours */
return Math.abs(start - end)/36e5;
}
}
Bitte zeigen Sie uns einige Code, Sie tun die Arbeit, wir werden helfen. – Grumpy