Blog
Wild & Free Tools

Unix Timestamps in Moment.js, Day.js, and date-fns

Last updated: April 2026 6 min read

Table of Contents

  1. Moment.js
  2. Day.js
  3. date-fns
  4. Frequently Asked Questions

Native JavaScript Date is enough for most timestamp work, but the format string ergonomics are bad and timezone handling is worse. The three libraries that fix this are Moment.js (legacy, in maintenance mode), Day.js (modern, 2KB), and date-fns (functional, tree-shakable). All three handle Unix timestamps with one-line conversions.

This guide shows the syntax for each, then helps you pick which one to use in 2026.

Moment.js — Legacy But Still Common

Moment.js is in maintenance mode and the project recommends new code use other libraries. But it is still in millions of codebases and the API is what most JavaScript developers learned first.

// From Unix seconds
const m = moment.unix(1711000000);
m.format('YYYY-MM-DD HH:mm:ss'); // "2024-03-21 03:46:40"

// From milliseconds
const m2 = moment(1711000000123);

// To Unix seconds
const ts = moment().unix();
const ts2 = moment('2024-03-21').unix();

// In a specific timezone (requires moment-timezone)
moment.unix(1711000000).tz('America/New_York').format();

Moment.js bundles around 70KB minified plus another 35KB for the timezone data. That is heavy for a date library — which is why Day.js and date-fns exist.

Sell Custom Apparel — We Handle Printing & Free Shipping

Day.js — Drop-In Moment Replacement (2KB)

Day.js has nearly the same API as Moment.js but ships at about 2KB minified. For new projects, this is the right pick if you like the chainable Moment style.

import dayjs from 'dayjs';

// From Unix seconds
const d = dayjs.unix(1711000000);
d.format('YYYY-MM-DD HH:mm:ss'); // "2024-03-21 03:46:40"

// From milliseconds (default constructor)
const d2 = dayjs(1711000000123);

// To Unix seconds
const ts = dayjs().unix();
const ts2 = dayjs('2024-03-21').unix();

// Timezones (requires the timezone plugin)
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.unix(1711000000).tz('America/New_York').format();

The plugin pattern keeps the core small. You only load timezone support if you need it. For most apps that just need format and parse, the 2KB core is enough.

date-fns — Functional, Tree-Shakable

date-fns takes a different design approach: instead of a chainable wrapper object, it gives you pure functions that take a Date and return a Date or string. This makes it tree-shakable — you only bundle the functions you actually import.

import { fromUnixTime, format, getUnixTime } from 'date-fns';

// From Unix seconds
const date = fromUnixTime(1711000000);
format(date, 'yyyy-MM-dd HH:mm:ss'); // "2024-03-21 03:46:40"

// From milliseconds (use new Date)
const date2 = new Date(1711000000123);

// To Unix seconds
const ts = getUnixTime(new Date());
const ts2 = getUnixTime(new Date('2024-03-21'));

// Timezones (separate package: date-fns-tz)
import { utcToZonedTime, format } from 'date-fns-tz';
const zoned = utcToZonedTime(fromUnixTime(1711000000), 'America/New_York');

Bundle size depends on what you import. A typical app using format, parse, and a few helpers comes in around 10-15KB after tree-shaking — bigger than Day.js but smaller than Moment.

Which one should you pick?

LibraryPick when
Moment.jsExisting codebase already uses it. Avoid for new projects.
Day.jsYou want a Moment-style chainable API at 2KB. Best default for most apps.
date-fnsYou prefer functional code or want tree-shaking. Best for libraries and SSR.
None (native Date)You only need basic conversions. Fine for simple apps.

For one-off conversions outside of code, the free Unix timestamp converter handles it in your browser.

Try It Free — No Signup Required

Runs 100% in your browser. No data is collected, stored, or sent anywhere.

Open Free Unix Timestamp Converter

Frequently Asked Questions

How do I convert a Unix timestamp in Moment.js?

moment.unix(timestamp) for seconds-based, or moment(timestamp) for milliseconds-based (the default constructor expects milliseconds). To go back: moment().unix() returns the current Unix timestamp in seconds.

What is the difference between Day.js and Moment.js?

Day.js has nearly identical API to Moment.js but ships at 2KB instead of 70KB. Moment is in maintenance mode; Day.js is the recommended replacement. Day.js also uses a plugin pattern so you only load features you need.

Should I still use Moment.js in 2026?

Only if your existing codebase already uses it heavily and migrating is not worth the effort. For new projects, Day.js or date-fns are both better choices. The Moment team itself recommends switching.

How do I convert a Unix timestamp with date-fns?

Use fromUnixTime(timestamp) which returns a regular Date object. Then pass it to format(date, "yyyy-MM-dd") for string output. Going back: getUnixTime(new Date()) gives the current Unix timestamp.

Which JavaScript date library has the smallest bundle size?

Day.js core is about 2KB minified — the smallest of the three. date-fns is tree-shakable so the size depends on what you import (typically 10-15KB for common usage). Moment.js is the largest at around 70KB even after minification.

Do I need a date library at all in 2026?

For basic conversions, no — native JavaScript Date is enough. Use a library when you need: complex format strings (date-fns format is much better than Date.toLocaleString), timezone handling beyond what Date provides natively, or relative time formatting ("3 days ago").

Launch Your Own Clothing Brand — No Inventory, No Risk