Moment.js Playground
Interactive playground for experimenting with Moment.js date and time API - format, parse, manipulate dates with live output
Input
Output
Readme
What is Moment.js?
Moment.js is a widely-used JavaScript library for parsing, validating, manipulating, and formatting dates. Before modern alternatives emerged, Moment.js was the de facto standard for date handling in JavaScript applications. It provides a clean, chainable API that makes working with dates intuitive and straightforward. While newer libraries like date-fns and Luxon exist, Moment.js remains heavily used in legacy codebases and is still actively maintained for bug fixes. Understanding Moment.js is essential for developers working with existing projects that rely on it.
Why use a date manipulation library?
JavaScript's native Date object is notoriously difficult to work with. It has inconsistent behavior across browsers, confusing month indexing (0-11 instead of 1-12), limited formatting options, and no built-in timezone support. Date libraries like Moment.js solve these problems by providing a consistent, well-documented API. They handle edge cases like daylight saving time transitions, leap years, and locale-specific formatting automatically, saving developers from writing error-prone date logic from scratch.
Tool description
The Moment.js Playground is an interactive browser-based environment for experimenting with the Moment.js library. Write and execute JavaScript code directly in the editor with full access to Moment.js and Moment Timezone. The playground automatically runs your code as you type (with debouncing) and displays all console output in a terminal-style panel below. It's perfect for learning Moment.js, testing date formatting strings, debugging date calculations, and prototyping time-related logic without setting up a development environment.
Examples
Format the current date:
const now = moment();
console.log(now.format("MMMM Do, YYYY"));
// Output: December 30th, 2025
console.log(now.format("dddd, h:mm A"));
// Output: Monday, 3:45 PMParse different date formats:
const date1 = moment("2025-12-25", "YYYY-MM-DD");
const date2 = moment("25/12/2025", "DD/MM/YYYY");
const date3 = moment("Dec 25, 2025", "MMM D, YYYY");
console.log(date1.format("dddd")); // Thursday
console.log(date2.isValid()); // true
console.log(date3.month()); // 11 (0-indexed)Calculate relative time:
const past = moment().subtract(3, "days");
const future = moment().add(2, "weeks");
console.log(past.fromNow()); // 3 days ago
console.log(future.fromNow()); // in 14 days
console.log(past.from(future)); // 17 days agoDate arithmetic:
const date = moment("2025-01-15");
const nextMonth = date.clone().add(1, "month");
const lastWeek = date.clone().subtract(1, "week");
const endOfMonth = date.clone().endOf("month");
console.log(nextMonth.format("YYYY-MM-DD")); // 2025-02-15
console.log(lastWeek.format("YYYY-MM-DD")); // 2025-01-08
console.log(endOfMonth.format("YYYY-MM-DD")); // 2025-01-31Work with durations:
const start = moment("2025-01-01");
const end = moment("2025-12-31");
const duration = moment.duration(end.diff(start));
console.log(duration.asDays() + " days"); // 364 days
console.log(duration.asWeeks() + " weeks"); // 52 weeks
console.log(duration.humanize()); // a yearCompare dates:
const date1 = moment("2025-06-15");
const date2 = moment("2025-12-25");
console.log(date1.isBefore(date2)); // true
console.log(date1.isAfter(date2)); // false
console.log(date1.isSame(date2, "year")); // true
console.log(date1.isBetween("2025-01-01", "2025-08-01")); // trueWork with timezones:
const now = moment();
console.log(now.tz("America/New_York").format("h:mm A z"));
console.log(now.tz("Europe/London").format("h:mm A z"));
console.log(now.tz("Asia/Tokyo").format("h:mm A z"));Features
- Full Moment.js Library: Access all Moment.js functions including parsing, formatting, manipulation, and comparison
- Timezone Support: Moment Timezone is pre-loaded for working with time zones worldwide
- Live Code Execution: Code runs automatically as you type with smart debouncing
- Built-in Terminal: View console output instantly without opening browser dev tools
- Syntax Highlighting: JavaScript syntax highlighting for better code readability
- Error Handling: Runtime errors are displayed clearly in the terminal output
Use cases
- Learning Moment.js: Experiment with the API and see results immediately without project setup
- Format String Testing: Try different format tokens to achieve the exact date output you need
- Date Calculation Debugging: Test complex date arithmetic before implementing in your application
- Timezone Conversions: Quickly convert times between different time zones
- Legacy Code Maintenance: Test Moment.js code snippets when maintaining existing applications
Common Format Tokens
| Token | Description | Example |
|---|---|---|
YYYY |
4-digit year | 2025 |
YY |
2-digit year | 25 |
MM |
2-digit month | 01-12 |
MMM |
Short month name | Jan, Feb |
MMMM |
Full month name | January |
DD |
2-digit day | 01-31 |
Do |
Day with ordinal | 1st, 2nd, 3rd |
dddd |
Full weekday name | Monday |
ddd |
Short weekday | Mon |
HH |
24-hour hour | 00-23 |
hh |
12-hour hour | 01-12 |
mm |
Minutes | 00-59 |
ss |
Seconds | 00-59 |
A |
AM/PM (uppercase) | AM, PM |
a |
am/pm (lowercase) | am, pm |
z |
Timezone abbreviation | EST, PST |
Z |
Timezone offset | +05:00 |
Common Methods Reference
| Method | Description | Example |
|---|---|---|
moment() |
Create current moment | moment() |
format(string) |
Format to string | moment().format("YYYY-MM-DD") |
add(n, unit) |
Add time | moment().add(7, "days") |
subtract(n, unit) |
Subtract time | moment().subtract(1, "month") |
startOf(unit) |
Start of unit | moment().startOf("week") |
endOf(unit) |
End of unit | moment().endOf("month") |
fromNow() |
Relative time | moment("2025-01-01").fromNow() |
diff(moment) |
Difference | momentA.diff(momentB, "days") |
isBefore(moment) |
Is before | momentA.isBefore(momentB) |
isAfter(moment) |
Is after | momentA.isAfter(momentB) |
isSame(moment, unit) |
Is same | momentA.isSame(momentB, "month") |
isValid() |
Check validity | moment("invalid").isValid() |
clone() |
Create copy | moment().clone() |
tz(zone) |
Convert timezone | moment().tz("America/New_York") |
Time Units
Moment.js supports these time units for add, subtract, startOf, endOf, and diff operations:
years/yquarters/Qmonths/Mweeks/wdays/dhours/hminutes/mseconds/smilliseconds/ms