JSFiddle - React, Tailwind, and code Playground

by brigand

JavaScript

const spans = [
  { cost: 10000, start: Date.parse('2020-06-01T00:00Z'), end: Date.parse('2020-06-10T00:00Z') },
  { cost: 20000, start: Date.parse('2020-06-10T00:00Z'), end: Date.parse('2020-06-30T23:59Z') },
];

const percentage = (span) => {
  const periodStart = spans[0].start;
  const periodEnd = spans.slice().pop().end;

  
  return (span.end - span.start) / (periodEnd - periodStart);
}


const cost = (span) => {
	console.log(percentage(span))
	return percentage(span) * span.cost;
}

const total = spans.map(cost).reduce((a, b) => a + b);
console.log(Math.round(total) / 100);