Skip to content

Instantly share code, notes, and snippets.

@brennie
Created June 9, 2019 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennie/3d88514be66c96bdec534f30615aa4d7 to your computer and use it in GitHub Desktop.
Save brennie/3d88514be66c96bdec534f30615aa4d7 to your computer and use it in GitHub Desktop.
Tabulate total time spend on Toronto Bikeshare
(function () {
const days = new Map();
Array.prototype.forEach.call(
document.querySelectorAll('tbody tr'),
el => {
const start = new Date(el.querySelector('td:nth-child(3)').textContent);
const end = new Date(el.querySelector('td:nth-child(5)').textContent);
const duration = (end - start) / 1000; // in seconds
const day = `${start.getFullYear()}-${start.getMonth() + 1}-${start.getDate()}`;
days.set(day, (days.get(day) || 0) + duration);
});
const displayTime = t => {
const seconds = Math.floor(t % 60); t /= 60;
const minutes = Math.floor(t % 60); t /= 60;
const hours = Math.floor(t);
const strParts = [];
if (hours > 0) {
strParts.push(`${hours}h`);
}
if (minutes > 0) {
strParts.push(`${minutes}m`);
}
if (seconds > 0) {
strParts.push(`${seconds}s`);
}
return strParts.join(' ');
};
const table = {};
let total = 0;
for (const [day, time] of days.entries()) {
table[day] = { duration: displayTime(time) };
total += time;
}
table['total'] = { duration: displayTime(total) };
console.table(table);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment