I am trying to make a countdown timer for Christmas and found this video useful. It uses React with Day.js package. It works well if I set Christmas date timestamp manually. However, there are two target functions that I can't figure out.
Make timer disappear and display "Christmas today" on screen for 24 hours once it reaches the day of Christmas.
Automatically set new timeStampDayJs to next Christmas once current date reaches Dec. 26?
Currently, I have to set the next Christmas timestamp manually once Christmas arrives, and I can't automatically show "Christmas today" on the day of Christmas. Is there any way I can achieve the two goals automatically?
My code here (mostly the same as the YouTube video, but I set Christmas timestamp manually):
import { useState, useEffect } from "react";
import dayJs from "dayjs";
var utc = require("dayjs/plugin/utc");
var timezone = require("dayjs/plugin/timezone");
dayJs.extend(utc);
dayJs.extend(timezone);
const defaultRemainingTime = {
seconds: "00",
minutes: "00",
hours: "00",
days: "00",
};
const Countdown = () => {
const [remainingTime, setRemainingTime] = useState(defaultRemainingTime);
const countdownTimeStamp = 1671919200000;
const getRemainingTime = (countdownTimeStamp) => {
const timeStampDayJs = dayJs(countdownTimeStamp).tz("Europe/Helsinki");
const nowDayJs = dayJs().tz("Europe/Helsinki");
if (timeStampDayJs.isBefore(nowDayJs)) {
return defaultRemainingTime;
}
return {
seconds: getRemainingSeconds(nowDayJs, timeStampDayJs),
minutes: getRemainingMinutes(nowDayJs, timeStampDayJs),
hours: getRemainingHours(nowDayJs, timeStampDayJs),
days: getRemainingDays(nowDayJs, timeStampDayJs),
};
};
const getRemainingSeconds = (nowDayJs, timeStampDayJs) => {
const seconds = timeStampDayJs.diff(nowDayJs, "seconds") % 60;
return padWithZeros(seconds, 2);
};
const getRemainingMinutes = (nowDayJs, timeStampDayJs) => {
const minutes = timeStampDayJs.diff(nowDayJs, "minutes") % 60;
return padWithZeros(minutes, 2);
};
const getRemainingHours = (nowDayJs, timeStampDayJs) => {
const hours = timeStampDayJs.diff(nowDayJs, "hours") % 24;
return padWithZeros(hours, 2);
};
const getRemainingDays = (nowDayJs, timeStampDayJs) => {
const days = timeStampDayJs.diff(nowDayJs, "days");
return days;
};
const padWithZeros = (number, minLength) => {
const numberString = number.toString();
if (numberString.length >= minLength) {
return numberString;
} else {
return "0".repeat(minLength - numberString.length) + numberString;
}
};
useEffect(() => {
const intervalId = setInterval(() => {
updateRemaninigTime(countdownTimeStamp);
}, 1000);
return () => clearInterval(intervalId);
}, [countdownTimeStamp]);
const updateRemaninigTime = (countdownTimeStamp) => {
setRemainingTime(getRemainingTime(countdownTimeStamp));
};
return (
<div>
<span>{remainingTime.days} </span>
<span>days </span>
<span>{remainingTime.hours} </span>
<span>hours </span>
<span>{remainingTime.minutes} </span>
<span>minutes </span>
<span>{remainingTime.seconds} </span>
<span>seconds </span>
</div>
);
};
export default Countdown;