'Momentjs displaying wrong countdown time
I have this function that takes in two parameters: startTime and endTime(optional). If there is no endTime provided, it's counting from now to startTime. If startTime was in the past, it prefixes the countdown with + and is counting up, and if startTime is in the future, it will be prefixed with - and counting down. But if endTime is provided, it is a fixed time from startTime to endTime. Basically a mission countdown like you can see on SpaceX missions for example.
Now my question is, why is it displaying one day extra?
const countTimeMoment = (startTime: number, endTime?: number) => {
if (!startTime) return "No Data";
const timestamp = endTime && endTime != 0 ? moment(endTime) : moment();
const difference = moment(timestamp).diff(startTime);
const seconds = Math.floor(difference / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (difference < 0) {
return `-${-days}d ${-hours % 24}h ${-minutes % 60}m ${-seconds % 60}s`;
} else {
return `+${days}d ${hours % 24}h ${minutes % 60}m ${seconds % 60}s`;
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
