AI summary

Work out the days, hours and minutes left with Moment.js, see where the project stands, and write the same countdown with Date, Day.js or Temporal.

  • Moment.js is in maintenance mode. 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.js. 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.js 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.js to a new project today.

The Moment.js docs call it a “legacy project in maintenance mode”. 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.js.

If you already have code using Moment.js, there’s no rush to rip it out. Below are the Moment.js 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.

js
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.js moves whole months out of it, and days() only shows the days left over. The countdown then shows the wrong number.

asDays() versus days()These values are calculated from the duration code in Moment.js 2.31.0. Day.js 1.11.23 gives the same asDays() and days() values.
  1. Time left: 25 hours

    1 day and 1 hour left. Both are right.

    • Math.floor(asDays())1 / 400 days
    • days()1 / 30 days
  2. Time left: 31 days

    The duration passed a month, so days() dropped to zero.

    • Math.floor(asDays())31 / 400 days
    • days()0 / 30 days
  3. 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.

    • Math.floor(asDays())400 / 400 days
    • days()4 / 30 days

If you move this code to Day.js, don’t take the hours from hours(). Day.js 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 UTC. With an offset, every user counts down to the same moment. A date without one, like the Moment.js example above, is read in each user’s local time.

js
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:

One countdown, four toolsPick a tool. Each line is one step of the countdown.

Moment.js

In maintenance mode. I wouldn't start a new project with it.

  • moment("01/01/2027 00:00:00", format, true)
  • end.diff(moment())
  • moment.duration(ms).asDays()

Day.js

The API is very close to the one in Moment.js, but you have to enable the duration and customParseFormat plugins.

  • dayjs("01/01/2027 00:00:00", format, true) (changed)
  • end.diff(dayjs()) (changed)
  • dayjs.duration(ms).asDays() (changed)

Date

No dependency. You need to give the target in ISO 8601 form.

  • new Date("2027-01-01T00:00:00+03:00").getTime() (changed)
  • target - Date.now() (changed)
  • Math.floor(total / 86400) (changed)

Temporal

The language's new date API. It isn't in every browser, so add a polyfill if needed. An Instant difference only goes up to hours.

  • Temporal.Instant.from("2027-01-01T00:00:00+03:00") (changed)
  • Temporal.Now.instant().until(target, { largestUnit: "hours" }) (changed)
  • Math.floor(left.hours / 24) (changed)

Temporal is the language’s new built-in date API. TC39 has moved it to Stage 4, so it’s now part of the JavaScript standard.

In the Temporal 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.

Temporal 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 Temporal as Baseline yet.

So I wouldn’t rely on the browser alone today. A polyfill 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.

jsx
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 act”. My React testing article covers how to wait for updates like these.

What I’d pick

  • You already have Moment.js 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.js is the closest to Moment.js. Temporal needs a polyfill until Safari ships it.

References

  1. Moment.js documentation
  2. Moment.js news and project status
  3. Day.js Duration
  4. ECMAScript Date Time String Format
  5. MDN Temporal
  6. TC39 Temporal proposal
  7. MDN on setTimeout delays

Written by:

Full Stack Software EngineerSamsun, Türkiye

Full Stack Software Engineer. I started with client sites at an agency in 2019, then worked on embeds, a dashboard and analytics at a startup called Popupsmart.