📆 Day.js Cheatsheet
1️⃣ Installation
# Install via npm or yarn
npm install dayjs
yarn add dayjs
const dayjs = require('dayjs'); // CommonJS
import dayjs from 'dayjs'; // ES Module
2️⃣ Creating Dates
dayjs(); // Current date & time
dayjs("2025-02-20"); // Parse string
dayjs(1708387200000); // Unix timestamp (milliseconds)
dayjs.unix(1708387200); // Unix timestamp (seconds)
dayjs(new Date()); // JavaScript Date object
dayjs().format(); // "2025-02-20T15:30:00+09:00"
dayjs().format('YYYY-MM-DD'); // "2025-02-20"
dayjs().format('YYYY/MM/DD HH:mm:ss'); // "2025/02/20 15:30:00"
dayjs().format('[Today is] dddd'); // "Today is Thursday"
Token |
Output |
Example |
YYYY |
Year |
2025 |
MM |
Month (01-12) |
02 |
DD |
Day (01-31) |
20 |
ddd |
Short weekday |
Thu |
dddd |
Full weekday |
Thursday |
HH |
Hour (00-23) |
15 |
mm |
Minute (00-59) |
30 |
ss |
Second (00-59) |
45 |
A |
AM/PM |
PM |
4️⃣ Manipulating Dates
dayjs().add(1, 'day'); // Add 1 day
dayjs().subtract(3, 'month'); // Subtract 3 months
dayjs().startOf('year'); // Start of the year
dayjs().endOf('month'); // End of the month
Function |
Example |
.add(7, 'days') |
Adds 7 days |
.subtract(1, 'year') |
Subtracts 1 year |
.startOf('month') |
Beginning of the month |
.endOf('week') |
End of the week |
5️⃣ Comparing Dates
dayjs('2025-02-20').isBefore('2025-03-01'); // true
dayjs('2025-02-20').isAfter('2025-02-10'); // true
dayjs('2025-02-20').isSame('2025-02-20', 'day'); // true
6️⃣ Getting Differences
const d1 = dayjs('2025-02-20');
const d2 = dayjs('2025-03-01');
d2.diff(d1, 'day'); // 9 days
d2.diff(d1, 'hour'); // 216 hours
7️⃣ Working with Timezones (with Plugin)
npm install dayjs @dayjs/plugin/utc @dayjs/plugin/timezone
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs().tz("Asia/Seoul").format(); // "2025-02-20T15:30:00+09:00"
8️⃣ Relative Time (Time Ago)
npm install @dayjs/plugin/relativeTime
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);
dayjs("2025-02-10").fromNow(); // "10 days ago"
dayjs("2025-02-29").fromNow(); // "in 9 days"
9️⃣ Working with Locales
npm install @dayjs/locale/ko
import 'dayjs/locale/ko';
dayjs.locale('ko');
dayjs().format('dddd'); // "목요일"
dayjs("2025-02-20T10:00:00Z").utc().format(); // "2025-02-20T10:00:00Z"