JSFiddle - React, Tailwind, and code Playground
by lustre
HTML
<div id="parcelCounter">
<div class="thistime-day">Parcels today: </div>
<div class="thistime-week">This week: </div>
<div class="thistime-year">This year: </div>
</div>
JavaScript
$(function() {
/* Variables defined by the client */
var parcelsPerMinute = 34;
var parcelsPerDay = 50000;
var oneMinute = 60*1000; // seconds*milliseconds
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var thisYear = new Date().getFullYear();
var thisMonth = new Date().getMonth();
var thisDay = new Date().getDate();
var dayInWeek = new Date().getDay(); // Get the day of the week. 0=Sunday, 1=Monday
var startOfYear = new Date(thisYear,00,01); // Jan 1st of current year
var startOfToday = new Date(thisYear,thisMonth,thisDay,00,00,00); // start of the day
var today = new Date();
var diffMins = Math.round(Math.abs((startOfToday.getTime() - today.getTime())/(oneMinute))); // difference in minutes from start of the day to now.
var diffDays = Math.round(Math.abs((startOfYear.getTime() - today.getTime())/(oneDay)));
//var currentWeek = diffWeeks+1;
var parcelsSoFarToday = diffMins*parcelsPerMinute; // Number of parcels which have been delivered so far today. 34*number of minutes elapsed
/* 34 * number of minutes elapsed today */
$('.thistime-day').text(parcelsSoFarToday);
/* Calculate dayInWeek so Monday = 0. If it's less than 1 then day is Sunday so set to 6 */
var realDayInWeek = dayInWeek-1;
if(realDayInWeek < 0){
realDayInWeek = 6;
}
/* Work out todays then add 50000 * days in week */
$('.thistime-week').text(parcelsSoFarToday + (realDayInWeek * parcelsPerDay));
/* parcelsSoFarToday + (diffDays * 50000) */
$('.thistime-year').text(parcelsSoFarToday + (diffDays*parcelsPerDay));
});