JSFiddle - React, Tailwind, and code Playground
by ryanbrill
JavaScript
// a and b are javascript Date objects
function dateDiffInDays(a, b) {
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
// Discard the time and time-zone information.
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc1 - utc2) / _MS_PER_DAY);
}
var billingCycleEnds = 24;
var today = new Date(2014, 10, 24);
var monthOffset = (today.getDate() <= billingCycleEnds) ? 1 : 0;
var startBillingDate = new Date(2014, 10, 24);
startBillingDate.setDate(billingCycleEnds);
startBillingDate.setMonth(startBillingDate.getMonth() - monthOffset);
var daysInBillingCycle = new Date(startBillingDate.getFullYear(), startBillingDate.getMonth() + 1, 0).getDate(); // setting the day to 0 gets the previous month, so we're adding +1 to the billing month.
var daysIntoBillingCycle = dateDiffInDays(today, startBillingDate) - 1;
var multiplier = (daysInBillingCycle - daysIntoBillingCycle) / daysInBillingCycle;