Skip to content

Replacements for Moment.js

Day.js

Day.js provides a similar API to Moment.js with a much smaller footprint.

Example:

ts
import moment from 'moment'
import dayjs from 'dayjs'

const now = moment() 
const now = dayjs() 

const formatted = moment().format('YYYY-MM-DD') 
const formatted = dayjs().format('YYYY-MM-DD') 

date-fns

date-fns offers tree-shakable functions for working with native JavaScript dates.

Example:

ts
import moment from 'moment'
import { addDays, format, subWeeks } from 'date-fns'

const formatted = moment().format('YYYY-MM-DD') 
const formatted = format(new Date(), 'yyyy-MM-dd') 

const tomorrow = moment().add(1, 'day') 
const tomorrow = addDays(new Date(), 1) 

const lastWeek = moment().subtract(1, 'week') 
const lastWeek = subWeeks(new Date(), 1) 

Luxon

Luxon is created by a Moment.js maintainer and offers powerful internationalization support.

Example:

ts
import moment from 'moment'
import { DateTime } from 'luxon'

const now = moment() 
const now = DateTime.now() 

const formatted = moment().format('YYYY-MM-DD') 
const formatted = DateTime.now().toFormat('yyyy-MM-dd') 

const tomorrow = moment().add(1, 'day') 
const tomorrow = DateTime.now().plus({ days: 1 }) 

Native JavaScript Date

For simple use cases, native JavaScript Date and Intl APIs may be sufficient:

ts
import moment from 'moment'

const formatted = moment().format('YYYY-MM-DD') 
const formatted = new Date().toISOString().split('T')[0] 

const localized = moment().format('MMMM Do YYYY') 
const localized = new Intl.DateTimeFormat('en-US', { 
  year: 'numeric', 
  month: 'long', 
  day: 'numeric'
}).format(new Date()) 

Released under the MIT License. (68c82e22)