AI summary
Work out the days, hours and minutes left with Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode., see where the project stands, and write the same countdown with Date, Day.jsA small JavaScript date library with an API very close to the one in Moment.js. Extra features, such as durations, come as plugins you turn on. or TemporalJavaScript's new built-in API for dates, times and time zones. Where the browser supports it, you don't need a library..
- Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. is in maintenance modeA project in maintenance mode gets no new features. Its maintainers still fix critical security problems and may ship small maintenance releases.. It keeps working in existing projects, but I wouldn't add it to a new one.
- A countdown doesn't need a library. JavaScript's built-in Date and a little arithmetic are enough.
- For the total days use asDays() in Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode.. days() only counts the days left over after whole months, so it drops back to 0 after about 30.
I wrote this countdown with Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. in 2022. It shows the days, hours, minutes and seconds left until a target date. The code still works, but I wouldn’t add Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. to a new project today.
The Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. docs call it a “legacy project in maintenance modeA project in maintenance mode gets no new features. Its maintainers still fix critical security problems and may ship small maintenance releases.”. The maintainers’ August 2026 status update says they aren’t accepting new features. They still fix critical security problems.
Back in 2020 the same team asked new projects to pick other date libraries, such as Luxon and Day.jsA small JavaScript date library with an API very close to the one in Moment.js. Extra features, such as durations, come as plugins you turn on..
If you already have code using Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode., there’s no rush to rip it out. Below are the Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. version and a version with plain Date.
Time left with Moment.js
I parse the end time with an explicit format in strict mode. With true as the third argument, the input has to match the format exactly, separators included. A string like 01-01-2027 00:00:00 then counts as invalid.
import moment from "moment";
const end = moment("01/01/2027 00:00:00", "DD/MM/YYYY HH:mm:ss", true);
function remaining() {
const left = moment.duration(Math.max(0, end.diff(moment())));
return {
days: Math.floor(left.asDays()),
hours: left.hours(),
minutes: left.minutes(),
seconds: left.seconds(),
};
}end.diff(moment()) gives the milliseconds left. moment.duration() turns them into a duration you can read in parts.
There’s a distinction here that’s easy to miss. hours(), minutes() and seconds() each give one part of the duration, for example the 5 in “400 days and 5 hours”. asDays() gives the whole duration in days.
days() looks like the one you want, but it’s also a part. Once the duration passes about 30 days, Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. moves whole months out of it, and days() only shows the days left over. The countdown then shows the wrong number.
Time left: 25 hours
1 day and 1 hour left. Both are right.
Time left: 31 days
The duration passed a month, so days() dropped to zero.
Time left: 400 days
days() is what's left after taking out the months and the year. The total you want on a countdown is 400.
If you move this code to Day.jsA small JavaScript date library with an API very close to the one in Moment.js. Extra features, such as durations, come as plugins you turn on., don’t take the hours from hours(). Day.jsA small JavaScript date library with an API very close to the one in Moment.js. Extra features, such as durations, come as plugins you turn on. splits a long duration into parts differently: at 31 days left, its hours() gives 14 instead of 0. Work the hours out from the total, as the Date version below does.
With Date, no library
In a new project I’d write this with JavaScript’s built-in Date.
Give Date the target as an ISO 8601 string, like 2027-01-01T00:00:00+03:00. ISO 8601 is a standard date format. The JavaScript spec defines a simplified version of it, and every browser has to read that version the same way.
The +03:00 at the end is the time zone offset: how far that time is from UTCThe reference time that time zones are measured from. +03:00 means three hours ahead of it.. With an offset, every user counts down to the same moment. A date without one, like the Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. example above, is read in each user’s local time.
const target = new Date("2027-01-01T00:00:00+03:00").getTime();
function remaining(target) {
const total = Math.max(0, Math.floor((target - Date.now()) / 1000));
return {
total,
days: Math.floor(total / 86400),
hours: Math.floor(total / 3600) % 24,
minutes: Math.floor(total / 60) % 60,
seconds: total % 60,
};
}Every version of the countdown has the same three steps: read the target date, subtract now from it, and turn the difference into days. Here are those three steps in each of the four tools:
TemporalJavaScript's new built-in API for dates, times and time zones. Where the browser supports it, you don't need a library. is the language’s new built-in date API. TC39The committee that decides what goes into JavaScript. A proposal moves through numbered stages, and Stage 4 means it's finished and part of the language. has moved it to Stage 4, so it’s now part of the JavaScript standard.
In the TemporalJavaScript's new built-in API for dates, times and time zones. Where the browser supports it, you don't need a library. version, Temporal.Instant is one exact moment with no calendar or time zone attached. Without a time zone it can’t know how long a day is, since some days have 23 or 25 hours when the clocks change for daylight saving time. So the difference stops at hours, and you divide the hours by 24 yourself.
TemporalJavaScript's new built-in API for dates, times and time zones. Where the browser supports it, you don't need a library. ships in Firefox 139, Chrome 144 and Node 26. Safari has it only in the 27.3 beta so far, and MDN doesn’t count TemporalJavaScript's new built-in API for dates, times and time zones. Where the browser supports it, you don't need a library. as BaselineMDN's label for a web feature that works in the current versions of all the major browsers. yet.
So I wouldn’t rely on the browser alone today. A polyfillA small library that adds a missing built-in API to browsers that don't have it yet, so the same code runs everywhere. such as @js-temporal/polyfill or temporal-polyfill fills the gap.
The countdown in React
setInterval doesn’t fire exactly on time, and browsers can delay timers in background tabs. So instead of subtracting a second each time the timer fires, I recalculate the time left from the target date.
useEffect(() => {
const target = new Date(eventTime).getTime();
if (Number.isNaN(target)) return;
const tick = () => {
const left = remaining(target);
setCounter(left);
if (left.total === 0) clearInterval(timer);
};
const timer = setInterval(tick, 1000);
tick();
return () => clearInterval(timer);
}, [eventTime]);When eventTime changes or the component leaves the page, the function returned from useEffect clears the old interval. For two-digit hours and minutes on screen, String(value).padStart(2, "0") is enough.
In a test, this component’s state changes because a timer fired, not because of anything the test did. React then warns that an update was “not wrapped in actA React test helper. When code that changes a component's state runs inside act(), React finishes those updates before the test checks the result.”. My React testing article covers how to wait for updates like these.
What I’d pick
- You already have Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode. code: leave it, and make sure total days use
asDays(). - A new project that only needs a countdown:
Date, as above. - A new project with more date work: Day.jsA small JavaScript date library with an API very close to the one in Moment.js. Extra features, such as durations, come as plugins you turn on. is the closest to Moment.jsA JavaScript library for reading, formatting and calculating with dates. It was the usual choice for years and is now in maintenance mode.. TemporalJavaScript's new built-in API for dates, times and time zones. Where the browser supports it, you don't need a library. needs a polyfillA small library that adds a missing built-in API to browsers that don't have it yet, so the same code runs everywhere. until Safari ships it.
