Skip to content

Instantly share code, notes, and snippets.

@jonsamp
Created April 17, 2020 02:44
Show Gist options
  • Save jonsamp/fc852ae31d89aa7892391dcc1d6667e9 to your computer and use it in GitHub Desktop.
Save jonsamp/fc852ae31d89aa7892391dcc1d6667e9 to your computer and use it in GitHub Desktop.
const arguments = [...process.argv];
const actualMiles = arguments[2];
const date = arguments[3];
function dayOfYear() {
let now = new Date();
if (date) {
now = new Date(date);
}
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}
function format(date, y) {
var z = {
M: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
m: date.getMinutes(),
s: date.getSeconds()
};
y = y.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
return ((v.length > 1 ? "0" : "") + eval("z." + v.slice(-1))).slice(-2);
});
return y.replace(/(y+)/g, function(v) {
return date
.getFullYear()
.toString()
.slice(-v.length);
});
}
function dateFromDay(day) {
var date = new Date(2020, 0); // initialize a date in `year-01-01`
return format(new Date(date.setDate(day)), "MM/dd/yyyy"); // add the number of days
}
function milesPerDay() {
// 2020 is a leap year!
return 1000 / 366;
}
function goalForToday() {
return milesPerDay() * dayOfYear();
}
function compareActualMiles() {
if (!actualMiles) {
return;
}
console.log(
`Actual: ${actualMiles} miles (${(actualMiles / 10).toFixed(2)}% of goal)`
);
}
function milesFromTarget() {
if (!actualMiles) {
return;
}
const difference = (goalForToday() - actualMiles).toFixed(2);
const daysDifference = (Math.abs(difference) / milesPerDay()).toFixed(2);
const status = difference >= 0 ? "BEHIND" : "AHEAD of";
console.log(
`\n🏃‍♂️ ${Math.abs(difference)} miles (${daysDifference} days) ${status} pace`
);
}
function estimatedEndDate() {
if (!actualMiles) {
return;
}
const percentOfGoal = goalForToday() / actualMiles;
const finishDayOfYear = (percentOfGoal * 366).toFixed();
console.log(`🎯 Estimated finish date: ${dateFromDay(finishDayOfYear)}`);
}
console.log(
`Goal: ${goalForToday().toFixed(2)} miles (${(goalForToday() / 10).toFixed(
2
)}% of goal)`
);
compareActualMiles();
milesFromTarget();
estimatedEndDate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment