retropilot-client/src/controllers/helpers.js

21 lines
669 B
JavaScript
Raw Normal View History

2022-01-07 10:24:35 -07:00
export function formatDate(timestampMs) {
2022-01-07 10:59:15 -07:00
return new Date(timestampMs).toISOString().replace(/T/, ' ').replace(/\..+/, '');
}
2022-01-07 10:24:35 -07:00
2022-01-07 10:59:15 -07:00
export function formatDuration(durationSeconds) {
2022-01-09 15:56:12 -07:00
const durationSecondsRound = Math.round(durationSeconds);
const secs = durationSecondsRound % 60;
let mins = Math.floor(durationSecondsRound / 60);
2022-01-07 10:59:15 -07:00
let hours = Math.floor(mins / 60);
2022-01-09 09:52:29 -07:00
mins %= 60;
2022-01-07 10:59:15 -07:00
const days = Math.floor(hours / 24);
2022-01-09 09:52:29 -07:00
hours %= 24;
2022-01-07 10:59:15 -07:00
let response = '';
2022-01-09 09:52:29 -07:00
if (days > 0) response += `${days}d `;
if (hours > 0 || days > 0) response += `${hours}h `;
if (hours > 0 || days > 0 || mins > 0) response += `${mins}m `;
response += `${secs}s`;
2022-01-07 10:59:15 -07:00
return response;
2022-01-09 09:52:29 -07:00
}