Skip to content

Instantly share code, notes, and snippets.

@ogiovannyoliveira
Created July 31, 2020 14:12
Show Gist options
  • Save ogiovannyoliveira/9c286252d14f969ca6019863d46b5118 to your computer and use it in GitHub Desktop.
Save ogiovannyoliveira/9c286252d14f969ca6019863d46b5118 to your computer and use it in GitHub Desktop.
Function that returns the percentage of the time elapsed between two dates
export const capturarPercentualTempoDecorridoPasso = (data_inicial: Date | null, data_final: Date | null, tempo_padrao: number): number => {
if (!data_inicial) {
return 0
}
const hs3 = 10800
const segundosTotal = tempo_padrao * 60 * 60
const inicio = moment(data_inicial).unix() - hs3
const fim = moment(data_final).unix() - hs3
if (data_final) {
const diferenca = fim - inicio
if (diferenca < segundosTotal) {
const res = ((diferenca * 100) / segundosTotal).toFixed(0)
return isNaN(Number(res)) ? Number(0) : Number(res)
} else if (diferenca >= segundosTotal) {
return 100
}
}
const agora = moment().unix() - hs3
const intervalo = agora - inicio
if (intervalo < segundosTotal) {
const result = ((intervalo * 100) / segundosTotal).toFixed(0)
return isNaN(Number(result)) ? Number(0) : Number(result)
} else if (intervalo >= segundosTotal) {
return 100
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment