Skip to content

Instantly share code, notes, and snippets.

@jsjoeio
Last active March 31, 2021 20:26
Show Gist options
  • Save jsjoeio/30198ac092a3afd9bea9e475af0da878 to your computer and use it in GitHub Desktop.
Save jsjoeio/30198ac092a3afd9bea9e475af0da878 to your computer and use it in GitHub Desktop.
Calculate Fitbit Monthly Stats - Duration

Calculate Fitbit Monthly Stats - Duration

Unfortunately, the "Track My Activities on Fitbit" doesn't calculate the total hours (using duration) so I wrote a script that you can run in the console on the page to calculate the total.

function calculateFitBitMonthlyStats() {
// 1. Grab the table inside the div with class "history"
const logs = document.querySelector("div.history table.logs")
const today = new Date();
const CURRENT_MONTH = today.getMonth();
// 2. Looking at the children of the logs, grab only the January logs
let jLogs = Array.from(logs.children).filter(log => {
// we need to look at the date
// logs.children[1].children[0].children[0].textContent
const date = log.children[0].children[0].textContent
// only return the January ones
return date.includes(CURRENT_MONTH)
})
function convertDurationToHours(duration) {
// split on the :
// reverse it and then add
if (!duration) return 0
const split = duration.split(":").reverse()
const secondsInHours = parseInt(split[0])/3600
const minutesInHours = parseInt(split[1])/60
const hours = split.length === 3 ? parseInt(split[2]): 0
return secondsInHours + minutesInHours + hours
}
// 3. Sum them up
var h = 0
jLogs.forEach(log => {
// get the duration
// this will be a string like "15:22"
// we want to convert it to hours
const duration = log.children[0].children[4]?.innerHTML
const hours = (duration !== "undefined" && duration !== null) ? convertDurationToHours(duration) : 0
h += hours
})
console.log(`Total Hours for ${CURRENT_MONTH}: ${h}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment