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.
Creating Dates 5
moment()moment("2024-01-15")moment("2024-01-15", "YYYY-MM-DD")moment(Date.now())moment({ year: 2024, month: 0, day: 15 })Formatting 5
moment().format("YYYY-MM-DD")moment().format("HH:mm:ss")moment().format("dddd, MMMM Do YYYY")moment().format("LLL")moment().toISOString()Manipulating Dates 5
moment().add(7, "days")moment().subtract(1, "month")moment().startOf("day")moment().endOf("month")moment().year(2025).month(0)Queries & Comparison 8
moment().isBefore(other)moment().isAfter(other)moment().isSame(other, "day")moment().isBetween(start, end)moment().isSameOrBefore(other)moment().isSameOrAfter(other)moment().diff(other, "days")moment().isValid()Relative Time 6
moment().fromNow()moment().fromNow(true)moment().toNow()moment().calendar()moment().from(other)moment().to(other)Timezones & Localization 5
moment().utc()moment().local()moment.locale("zh-cn")moment.tz("2024-01-15", "America/New_York")moment().utcOffset(480)Duration 5
moment.duration(100, "minutes")moment.duration({ hours: 2, minutes: 30 })duration.asHours()duration.humanize()duration.add(1, "hour")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