Moment.js Cheatsheet - Date & Time

This reference is for JavaScript developers doing everyday date work — formatting a timestamp for display, adding days to an expiry, comparing two dates, or showing "3 days ago". It walks creation from strings, timestamps, and object literals (mind the zero-based month), formatting tokens, add/subtract and startOf/endOf snapping, comparison helpers, relative time, and timezone handling with moment-timezone. Note that moment is in maintenance mode; prefer native Date or Day.js for new code. After reading you should be able to parse, format, shift, and compare dates, and render friendly relative strings.

Languages·39 commands·Last updated 2026-07-21
momentDatesTimeFormatting

Creating Dates 5

moment()
Current time
moment("2024-01-15")
Parse from a string
moment("2024-01-15", "YYYY-MM-DD")
Parse with an explicit format
moment(Date.now())
Create from a timestamp
moment({ year: 2024, month: 0, day: 15 })
Create from an object

Formatting 5

moment().format("YYYY-MM-DD")
2024-01-15
moment().format("HH:mm:ss")
14:30:00
moment().format("dddd, MMMM Do YYYY")
Monday, January 15th 2024
moment().format("LLL")
Localized long format
moment().toISOString()
ISO 8601 format

Manipulating Dates 5

moment().add(7, "days")
Add 7 days
moment().subtract(1, "month")
Subtract 1 month
moment().startOf("day")
Set to start of day (00:00:00)
moment().endOf("month")
Set to end of month (23:59:59)
moment().year(2025).month(0)
Set year and month

Queries & Comparison 8

moment().isBefore(other)
Whether before another
moment().isAfter(other)
Whether after another
moment().isSame(other, "day")
Whether on the same day
moment().isBetween(start, end)
Whether within a range
moment().isSameOrBefore(other)
Same or before
moment().isSameOrAfter(other)
Same or after
moment().diff(other, "days")
Compute the difference (in a unit)
moment().isValid()
Whether a valid date

Relative Time 6

moment().fromNow()
3 days ago
moment().fromNow(true)
3 days (no 'ago')
moment().toNow()
in 3 days
moment().calendar()
Today 14:30 / Yesterday 09:00
moment().from(other)
Offset relative to another moment
moment().to(other)
Offset to another moment

Timezones & Localization 5

moment().utc()
Convert to UTC
moment().local()
Convert to local time
moment.locale("zh-cn")
Set Chinese locale
moment.tz("2024-01-15", "America/New_York")
Specify a timezone (needs moment-timezone)
moment().utcOffset(480)
Set a UTC offset (minutes)

Duration 5

moment.duration(100, "minutes")
Create a duration from minutes
moment.duration({ hours: 2, minutes: 30 })
Create a duration from an object
duration.asHours()
Convert to total hours
duration.humanize()
Human-friendly display (e.g. '2 hours')
duration.add(1, "hour")
Add to a duration

Tips

  • Moment.js is in maintenance mode; new projects should prefer dayjs (compatible API, smaller size).
  • Months are zero-indexed: 0 = January, 11 = December.
  • Format tokens: YYYY=year, MM=month, DD=day, HH=hour, mm=minute, ss=second.
  • Date operations return a new moment object; the original is immutable.
  • fromNow/toNow auto-pick a unit (seconds, minutes, hours, days, months, years).
  • diff returns milliseconds by default; pass a second arg for the unit (days/months/years).
  • Duration measures a length of time, not a point in time; humanize outputs friendly text.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us