you-dont-need/You-Dont-Need-Momentjs
{ "createdAt": "2018-09-08T14:57:38Z", "defaultBranch": "master", "description": "List of functions which you can use to replace moment.js + ESLint Plugin", "fullName": "you-dont-need/You-Dont-Need-Momentjs", "homepage": "", "language": "JavaScript", "name": "You-Dont-Need-Momentjs", "pushedAt": "2025-11-18T12:09:34Z", "stargazersCount": 13330, "topics": [ "bundle-size", "date-fns", "esmodules", "hacktoberfest", "moment-js", "pure-function", "tree-shaking" ], "updatedAt": "2025-11-25T16:15:39Z", "url": "https://github.com/you-dont-need/You-Dont-Need-Momentjs"}Moment.js is a fantastic time & date library with lots of great features and utilities. However, if you are working on a performance sensitive web application, it might cause a huge performance overhead because of its complex APIs and large bundle size.
![Large bundle size]!(./screenshot.png)
Problems with Moment.js:
- It is highly based on OOP APIs, which makes it fail to work with tree-shaking, thus leading to a huge bundle size and performance issues.
- It is mutable and it causes bugs:
- Complex OOP API (which doubles mutability problem). Here is an example:
https://github.com/moment/moment/blob/develop/src/test/moment/add_subtract.js#L244-L286
Moment.js allows to use
a.subtract('ms', 50),a.subtract(50, 'ms')and evena.subtract('s', '50').
If you are not using timezone but only a few simple functions from moment.js, this might bloat your app, and therefore is considered overkill. dayjs has a smaller core and has very similar APIs so it makes it very easy to migrate. date-fns enables tree-shaking and other benefits so that it works great with React, Sinon.js, and webpack, etc. See https://github.com/moment/moment/issues/2373 for more ideas on why and how people switch from moment.js to other solutions.
Brief Comparison
Section titled “Brief Comparison”| Name | Tree-shaking | Methods richness | Pattern | Locale | Timezone Support | Popularity (stars) | Sizes |
|---|---|---|---|---|---|---|---|
| Moment.js | No | High | OO | 123 | Good (moment-timezone) | ||
| Luxon | No | High | OO | - | Good (Intl) | ||
| date-fns | Yes | High | Functional | 64 | Good (date-fns-tz) | ||
| dayjs | No | High | OO | 138 | Good (Intl) |
Voice of Developers
Section titled “Voice of Developers”Removed moment.js to replace with date-fns - build output reduced by 40%
—Jared Farago from webnode project.
Good library if you’re looking to replace Moment.js for one reason or another. Immutable too.
—Dan Abramov, Author of Redux and co-author of Create React App. Building tools for humans.
—Matija Marohnić, a design-savvy frontend developer from Croatia.
ESLint Plugin
Section titled “ESLint Plugin”If you’re using ESLint, you can install a plugin that will help you identify places in your codebase where you don’t (may not) need Moment.js.
Install the plugin…
npm install --save-dev eslint-plugin-you-dont-need-momentjs…then update your config
"extends" : ["plugin:you-dont-need-momentjs/recommended"],Quick Links
Section titled “Quick Links”[Parse]!(#parse)
- [String + Date Format]!(#string—date-format)
- [String + Time Format]!(#string—time-format)
- [String + Format + locale]!(#string—format—locale)
[Get + Set]!(#get—set)
- [Millisecond/Second/Minute/Hour]!(#millisecond—second—minute—hour)
- [Date of Month]!(#date-of-month)
- [Day of Week]!(#day-of-week)
- [Day of Year]!(#day-of-year)
- [Week of Year]!(#week-of-year)
- [Days in Month]!(#days-in-month)
- [Weeks in Year]!(#weeks-in-year)
- [Maximum of the given dates]!(#maximum-of-the-given-dates)
- [Minimum of the given dates]!(#minimum-of-the-given-dates)
[Manipulate]!(#manipulate)
- [Add]!(#add)
- [Subtract]!(#subtract)
- [Start of Time]!(#start-of-time)
- [End of Time]!(#end-of-time)
[Display]!(#display)
- [Format]!(#format)
- [Time from now]!(#time-from-now)
- [Time from X]!(#time-from-x)
- [Difference]!(#difference)
[Query]!(#query)
- [Is Before]!(#is-before)
- [Is Same]!(#is-same)
- [Is After]!(#is-after)
- [Is Between]!(#is-between)
- [Is Leap Year]!(#is-leap-year)
- [Is a Date]!(#is-a-date)
Feature Parity
Section titled “Feature Parity”⚠️ Indicates other packages or work are needed. See individual functions [above]!(#quick-links).
| Native | Luxon | date-fns | dayjs | Temporal | |
|---|---|---|---|---|---|
| Parse | |||||
| String + Date Format | ✅ | ✅ | ✅ | ✅ | ✅ |
| String + Time Format | ✅ | ✅ | ✅ | ⚠️ | ✅ |
| String + Format + locale | ❌ | ⚠️ | ✅ | ⚠️ | ❌ |
| Get + Set | |||||
| Millisecond/Second/Minute/Hour | ✅ | ✅ | ✅ | ✅ | ✅ |
| Date of Month | ✅ | ✅ | ✅ | ✅ | ✅ |
| Day of Week | ✅ | ✅ | ✅ | ✅ | ✅ |
| Day of Year | ✅ | ✅ | ✅ | ✅ | ✅ |
| Week of Year | ✅ | ✅ | ✅ | ⚠️ | ✅ |
| Days in Month | ✅ | ✅ | ✅ | ✅ | ✅ |
| Weeks in Year | ❌ | ❌ | ✅ | ⚠️ | ✅ |
| Maximum of the given dates | ✅ | ✅ | ✅ | ⚠️ | ✅ |
| Minimum of the given dates | ✅ | ✅ | ✅ | ⚠️ | ✅ |
| Manipulate | |||||
| Add | ✅ | ✅ | ✅ | ✅ | ✅ |
| Subtract | ✅ | ✅ | ✅ | ✅ | ✅ |
| Start of Time | ❌ | ✅ | ✅ | ✅ | ✅ |
| End of Time | ✅ | ✅ | ✅ | ✅ | ✅ |
| Display | |||||
| Format | ✅ | ✅ | ✅ | ✅ | ✅ |
| Time from now | ✅ | ❌ | ✅ | ⚠️ | ✅ |
| Time from X | ❌ | ❌ | ✅ | ⚠️ | ✅ |
| Difference | ✅ | ✅ | ✅ | ✅ | ✅ |
| Query | |||||
| Is Before | ✅ | ✅ | ✅ | ✅ | ✅ |
| Is Same | ✅ | ✅ | ✅ | ✅ | ✅ |
| Is After | ✅ | ✅ | ✅ | ✅ | ✅ |
| Is Between | ❌ | ✅ | ✅ | ⚠️ | ❌ |
| Is Leap Year | ✅ | ✅ | ✅ | ⚠️ | ✅ |
| Is a Date | ✅ | ✅ | ✅ | ✅ | ✅ |
String + Date Format
Section titled “String + Date Format”Return the date parsed from date string using the given format string.
moment('12-25-1995', 'MM-DD-YYYY');// => "1995-12-24T13:00:00.000Z"
// Nativeconst datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;const [, month, day, year] = datePattern.exec('12-25-1995');new Date(`${month}, ${day} ${year}`);// => "1995-12-24T13:00:00.000Z"
// date-fnsimport parse from 'date-fns/parse';parse('12-25-1995', 'MM-dd-yyyy', new Date());// => "1995-12-24T13:00:00.000Z"
// dayjsdayjs('12-25-1995');// => "1995-12-24T13:00:00.000Z"
// luxonDateTime.fromFormat('12-25-1995', 'MM-dd-yyyy').toJSDate();// => "1995-12-24T13:00:00.000Z"
// Temporalconst datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;const [, month, day, year] = datePattern.exec('12-25-1995');new Temporal.ZonedDateTime.from({year, month, day, timeZone: Temporal.Now.timeZone()});// => "1995-12-24T13:00:00.000Z"[⬆ back to top]!(#quick-links)
String + Time Format
Section titled “String + Time Format”Return the date parsed from time string using the given format string.
moment('2010-10-20 4:30', 'YYYY-MM-DD HH:mm');// => "2010-10-19T17:30:00.000Z"
// Nativeconst datePattern = /^(\d{4})-(\d{2})-(\d{2})\s(\d{1,2}):(\d{2})$/;const [, year, month, day, rawHour, min] = datePattern.exec('2010-10-20 4:30');new Date(`${year}-${month}-${day}T${('0' + rawHour).slice(-2)}:${min}:00`);// => "2010-10-19T17:30:00.000Z"
// date-fnsimport parse from 'date-fns/parse';parse('2010-10-20 4:30', 'yyyy-MM-dd H:mm', new Date());// => "2010-10-19T17:30:00.000Z"
// dayjs ⚠️ requires customParseFormat pluginimport customParseFormat from 'dayjs/plugin/customParseFormat';dayjs.extend(customParseFormat);dayjs('2010-10-20 4:30', 'YYYY-MM-DD HH:mm');// => "2010-10-19T17:30:00.000Z"
// luxonDateTime.fromFormat('2010-10-20 4:30', 'yyyy-MM-dd H:mm').toJSDate();// => "2010-10-19T17:30:00.000Z"
// Temporalconst datePattern = /^(\d{4})-(\d{2})-(\d{2})\s(\d{1,2}):(\d{2})$/;const [, year, month, day, hour, minute] = datePattern.exec('2010-10-20 4:30');new Temporal.ZonedDateTime.from({year, month, day, hour, minute, timeZone: Temporal.Now.timeZone()});// => "2010-10-19T17:30:00.000Z"[⬆ back to top]!(#quick-links)
String + Format + locale
Section titled “String + Format + locale”Return the date parsed from string using the given format string and locale.
moment('2012 mars', 'YYYY MMM', 'fr');// => "2012-02-29T13:00:00.000Z"
// date-fnsimport parse from 'date-fns/parse';import fr from 'date-fns/locale/fr';parse('2012 mars', 'yyyy MMMM', new Date(), { locale: fr });// => "2012-02-29T13:00:00.000Z"
// dayjs ⚠️ requires customParseFormat pluginimport customParseFormat from 'dayjs/plugin/customParseFormat';import 'dayjs/locale/fr';dayjs.extend(customParseFormat);dayjs('2012 mars', 'YYYY MMM', 'fr');// => "2012-02-29T13:00:00.000Z"
// Luxon ❌ does not support Locale for node unless https://moment.github.io/luxon/docs/manual/install.html#nodeDateTime.fromFormat('2012 mars', 'yyyy MMMM', { locale: 'fr' });// => "2012-02-29T13:00:00.000Z"[⬆ back to top]!(#quick-links)
Get + Set
Section titled “Get + Set”Millisecond / Second / Minute / Hour
Section titled “Millisecond / Second / Minute / Hour”Get the Millisecond/Second/Minute/Hour of the given date.
moment().seconds();// => 49moment().hours();// => 19
// Nativenew Date().getSeconds();// => 49new Date().getHours();// => 19
// date-fnsimport getSeconds from 'date-fns/getSeconds';import getHours from 'date-fns/getHours';getSeconds(new Date());// => 49getHours(new Date());// => 19
// dayjsdayjs().second();// => 49dayjs().hour();// => 19
// LuxonDateTime.local().second;// => 49DateTime.local().hour;// => 19
// TemporalTemporal.Now.zonedDateTimeISO().second;// => 49Temporal.Now.zonedDateTimeISO().hour;// => 19Performance tests
Section titled “Performance tests”| Library | Time |
|---|---|
| Moment | 1500.703ms |
| Native | 348.411ms |
| DateFns | 520.670ms |
| DayJs | 494.234ms |
| Luxon | 1208.368ms |
| Temporal | - |
Set the Millisecond/Second/Minute/Hour of the given date.
moment().seconds(30);// => "2018-09-09T09:12:30.695Z"moment().hours(13);// => "2018-09-09T03:12:49.695Z"
// Nativenew Date(new Date().setSeconds(30));// => "2018-09-09T09:12:30.695Z"new Date(new Date().setHours(13));// => "2018-09-09T03:12:49.695Z"
// date-fnsimport setSeconds from 'date-fns/setSeconds';import setHours from 'date-fns/setHours';setSeconds(new Date(), 30);// => "2018-09-09T09:12:30.695Z"setHours(new Date(), 13);// => "2018-09-09T03:12:49.695Z"
// dayjsdayjs().set('second', 30);// => "2018-09-09T09:12:30.695Z"dayjs().set('hour', 13);// => "2018-09-09T03:12:49.695Z"
// luxonDateTime.utc() .set({ second: 30 }) .toJSDate();// => "2018-09-09T09:12:30.695Z"DateTime.utc() .set({ hour: 13 }) .toJSDate();// => "2018-09-09T03:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().with({ second: 30 });// => "2018-09-09T09:12:30.695Z"Temporal.Now.zonedDateTimeISO().with({ hour: 13 });// => "2018-09-09T03:12:49.695Z"Performance tests
Section titled “Performance tests”| Library | Time |
|---|---|
| Moment | 1689.744ms |
| Native | 636.741ms |
| DateFns | 714.148ms |
| DayJs | 2037.603ms |
| Luxon | 2897.571ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Date of Month
Section titled “Date of Month”Gets or sets the day of the month.
moment().date();// => 9moment().date(4);// => "2018-09-04T09:12:49.695Z"
// Nativenew Date().getDate();// => 9new Date().setDate(4);// => "2018-09-04T09:12:49.695Z"
// date-fnsimport getDate from 'date-fns/getDate';import setDate from 'date-fns/setDate';getDate(new Date());// => 9setDate(new Date(), 4);// => "2018-09-04T09:12:49.695Z"
// dayjsdayjs().date();// => 9dayjs().set('date', 4);// => "2018-09-04T09:12:49.695Z"
// luxonDateTime.utc().day;// => 9DateTime.utc() .set({ day: 4 }) .toString();// => "2018-09-04T09:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().day;// => 9Temporal.Now.zonedDateTimeISO().with({ day: 4 });// => "2018-09-04T09:12:49.695Z"Performance tests
Section titled “Performance tests”| Library | Time |
|---|---|
| Moment | 1381.669ms |
| Native | 397.415ms |
| DateFns | 588.004ms |
| DayJs | 1218.025ms |
| Luxon | 2705.606ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Day of Week
Section titled “Day of Week”Gets or sets the day of the week.
moment().day();// => 0 (Sunday)moment().day(-14);// => "2018-08-26T09:12:49.695Z"
// Nativenew Date().getDay();// => 0 (Sunday)new Date().setDate(new Date().getDate() - 14);// => "2018-08-26T09:12:49.695Z"
// date-fnsimport getDay from 'date-fns/getDay';import setDay from 'date-fns/setDay';getDay(new Date());// => 0 (Sunday)setDay(new Date(), -14);// => "2018-08-26T09:12:49.695Z"
// dayjsdayjs().day();// => 0 (Sunday)dayjs().set('day', -14);// => "2018-08-26T09:12:49.695Z"
// LuxonDateTime.local().weekday;// => 7 (Sunday)DateTime.local() .minus({ day: 14 }) .toJSDate();// => "2018-08-26T09:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().dayOfWeek;// => 7 (Sunday)Temporal.Now.zonedDateTimeISO().subtract(Temporal.Duration.from({ days: 14 }));// => "2018-09-04T09:12:49.695Z"| Library | Time |
|---|---|
| Moment | 1919.404ms |
| Native | 543.466ms |
| DateFns | 841.436ms |
| DayJs | 1229.475ms |
| Luxon | 3936.282ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Day of Year
Section titled “Day of Year”Gets or sets the day of the year.
moment().dayOfYear();// => 252moment().dayOfYear(256);// => "2018-09-13T09:12:49.695Z"
// NativeMath.floor( (new Date() - new Date(new Date().getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);// => 252
// date-fnsimport getDayOfYear from 'date-fns/getDayOfYear';import setDayOfYear from 'date-fns/setDayOfYear';getDayOfYear(new Date());// => 252setDayOfYear(new Date(), 256);// => "2018-09-13T09:12:49.695Z"
// dayjs ⚠️ requires dayOfYear pluginimport dayOfYear from 'dayjs/plugin/dayOfYear';dayjs.extend(dayOfYear);dayjs().dayOfYear();// => 252dayjs().dayOfYear(256);// => "2018-09-13T09:12:49.695Z"
// LuxonDateTime.local().ordinal;// => 252DateTime.local() .set({ ordinal: 256 }) .toString();// => "2018-09-13T09:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().dayOfYear;// => 252Temporal.Now.zonedDateTimeISO().with({month: 1, day: 1}).add(Temporal.Duration.from({days: 256}));// => "2018-09-04T09:12:49.695Z"| Library | Time |
|---|---|
| Moment | 5511.172ms |
| Native | 530.592ms |
| DateFns | 2079.043ms |
| DayJs | - |
| Luxon | 3540.810ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Week of Year
Section titled “Week of Year”Gets or sets the week of the year.
moment().week();// => 37moment().week(24);// => "2018-06-10T09:12:49.695Z"
// date-fnsimport getWeek from 'date-fns/getWeek';import setWeek from 'date-fns/setWeek';getWeek(new Date());// => 37setWeek(new Date(), 24);// => "2018-06-10T09:12:49.695Z"
// native getWeekconst day = new Date();const MILLISECONDS_IN_WEEK = 604800000;const firstDayOfWeek = 1; // monday as the first day (0 = sunday)const startOfYear = new Date(day.getFullYear(), 0, 1);startOfYear.setDate( startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7)));const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1;// => 37
// native setWeekconst day = new Date();const week = 24;const MILLISECONDS_IN_WEEK = 604800000;const firstDayOfWeek = 1; // monday as the first day (0 = sunday)const startOfYear = new Date(day.getFullYear(), 0, 1);startOfYear.setDate( startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7)));const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1;day.setDate(day.getDate() - (dayWeek - week) * 7);day.toISOString();// => "2018-06-10T09:12:49.794Z
// dayjs ⚠️ requires weekOfYear pluginimport weekOfYear from 'dayjs/plugin/weekOfYear';dayjs.extend(weekOfYear);dayjs().week();// => 37dayjs().week(24);// => "2018-06-10T09:12:49.695Z"
// LuxonDateTime.local().weekNumber;// => 37DateTime.local() .set({ weekNumber: 23 }) .toString();// => "2018-06-10T09:12:49.794Z
// TemporalTemporal.Now.zonedDateTimeISO().weekOfYear;// => 252Temporal.Now.zonedDateTimeISO().with({month: 1, day: 1}).add(Temporal.Duration.from({weeks: 23}));// => "2018-09-04T09:12:49.695Z"| Library | Time |
|---|---|
| Moment | 7147.201ms |
| Native | 1371.631ms |
| DateFns | 5834.815ms |
| DayJs | - |
| Luxon | 4514.771ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Days in Month
Section titled “Days in Month”Get the number of days in the current month.
moment('2012-02', 'YYYY-MM').daysInMonth();// => 29
// Nativenew Date(2012, 02, 0).getDate();// => 29
// date-fnsimport getDaysInMonth from 'date-fns/getDaysInMonth';getDaysInMonth(new Date(2012, 1));// => 29
// dayjsdayjs('2012-02').daysInMonth();// => 29
// LuxonDateTime.local(2012, 2).daysInMonth;// => 29
// Temporal(new Temporal.PlainYearMonth(2012, 2)).daysInMonth// orTemporal.PlainYearMonth.from('2012-02').daysInMonth// => 29| Library | Time |
|---|---|
| Moment | 4415.065ms |
| Native | 186.196ms |
| DateFns | 634.084ms |
| DayJs | 1922.774ms |
| Luxon | 1403.032ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Weeks in Year
Section titled “Weeks in Year”Gets the number of weeks in the current year, according to ISO weeks.
moment().isoWeeksInYear();// => 52
// Nativeconst year = new Date().getFullYear();const MILLISECONDS_IN_WEEK = 604800000;const firstMondayThisYear = new Date(+year, 0, 5-(new Date(+year, 0, 4).getDay()||7));const firstMondayNextYear = new Date(+year+1, 0, 5-(new Date(+year+1, 0, 4).getDay()||7));(firstMondayNextYear - firstMondayThisYear) / MILLISECONDS_IN_WEEK;// => 52
// date-fnsimport getISOWeeksInYear from 'date-fns/getISOWeeksInYear';getISOWeeksInYear(new Date());// => 52
// dayjs ⚠️ requires isoWeeksInYear pluginimport isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear';dayjs.extend(isoWeeksInYear);dayjs().isoWeeksInYear();// => 52
// LuxonDateTime.local().weeksInWeekYear;// => 52
// TemporalTemporal.PlainDate.from({day:31, month:12, year: Temporal.Now.plainDateISO()}).weekOfYear// => 52| Library | Time |
|---|---|
| Moment | 1065.247ms |
| Native | - |
| DateFns | 4954.042ms |
| DayJs | - |
| Luxon | 1134.483ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Maximum of the given dates
Section titled “Maximum of the given dates”Returns the maximum (most distant future) of the given date.
const array = [ new Date(2017, 4, 13), new Date(2018, 2, 12), new Date(2016, 0, 10), new Date(2016, 0, 9),];// Moment.jsmoment.max(array.map(a => moment(a)));// => "2018-03-11T13:00:00.000Z"
// Nativenew Date(Math.max.apply(null, array)).toISOString();// => "2018-03-11T13:00:00.000Z"
// date-fnsimport max from 'date-fns/max';max(array);// => "2018-03-11T13:00:00.000Z"
// dayjs ⚠️ requires minMax pluginimport minMax from 'dayjs/plugin/minMax';dayjs.extend(minMax);dayjs.max(array.map(a => dayjs(a)));// => "2018-03-11T13:00:00.000Z"
// LuxonDateTime.max(...array.map(a => DateTime.fromJSDate(a))).toJSDate();// => "2018-03-11T13:00:00.000Z"
// TemporalTemporal.Instant.fromEpochMilliseconds(Math.max.apply(null, array))// => "2018-03-11T13:00:00.000Z"| Library | Time |
|---|---|
| Moment | 1780.075ms |
| Native | 828.332ms |
| DateFns | 980.938ms |
| DayJs | - |
| Luxon | 2694.702ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Minimum of the given dates
Section titled “Minimum of the given dates”Returns the minimum (most distant future) of the given date.
const array = [ new Date(2017, 4, 13), new Date(2018, 2, 12), new Date(2016, 0, 10), new Date(2016, 0, 9),];// Moment.jsmoment.min(array.map(a => moment(a)));// => "2016-01-08T13:00:00.000Z"
// Nativenew Date(Math.min.apply(null, array)).toISOString();// => "2016-01-08T13:00:00.000Z"
// date-fnsimport min from 'date-fns/min';min(array);// => "2016-01-08T13:00:00.000Z"
// dayjs ⚠️ requires minMax pluginimport minMax from 'dayjs/plugin/minMax';dayjs.extend(minMax);dayjs.min(array.map(a => dayjs(a)));// => "2016-01-08T13:00:00.000Z"
// LuxonDateTime.min(...array.map(a => DateTime.fromJSDate(a))).toJSDate();// => "2016-01-08T13:00:00.000Z"
// TemporalTemporal.Instant.fromEpochMilliseconds(Math.min.apply(null, array))// => "2018-03-11T13:00:00.000Z"| Library | Time |
|---|---|
| Moment | 1744.459ms |
| Native | 819.646ms |
| DateFns | 841.249ms |
| DayJs | - |
| Luxon | 2720.462ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Manipulate
Section titled “Manipulate”Add the specified number of days to the given date.
moment().add(7, 'days');// => "2018-09-16T09:12:49.695Z"
// Nativeconst now = new Date();now.setDate(now.getDate() + 7);// => "Sun Sep 16 2018 09:12:49"
// date-fnsimport addDays from 'date-fns/addDays';addDays(new Date(), 7);// => "2018-09-16T09:12:49.695Z"
// dayjsdayjs().add(7, 'day');// => "2018-09-16T09:12:49.695Z"
// LuxonDateTime.local() .plus({ day: 7 }) .toJSDate();// => "2018-09-16T09:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().add(Temporal.Duration.from({days: 7}));// => "2018-09-16T09:12:49.695Z"| Library | Time |
|---|---|
| Moment | 1309.485ms |
| Native | 259.932ms |
| DateFns | 385.394ms |
| DayJs | 1911.881ms |
| Luxon | 3919.797ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Subtract
Section titled “Subtract”Subtract the specified number of days from the given date.
moment().subtract(7, 'days');// => "2018-09-02T09:12:49.695Z"
// Nativeconst now = new Date();now.setDate(now.getDate() - 7);// => Sun Sep 09 2018 09:12:49
// date-fnsimport subDays from 'date-fns/subDays';subDays(new Date(), 7);// => "2018-09-02T09:12:49.695Z"
// dayjsdayjs().subtract(7, 'day');// => "2018-09-02T09:12:49.695Z"
// LuxonDateTime.local() .minus({ day: 7 }) .toJSDate();// => "2018-09-02T09:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().subtract(Temporal.Duration.from({days: 7}));// => "2018-09-02T09:12:49.695Z"| Library | Time |
|---|---|
| Moment | 1278.384ms |
| Native | 215.255ms |
| DateFns | 379.057ms |
| DayJs | 1772.593ms |
| Luxon | 4028.866ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Start of Time
Section titled “Start of Time”Return the start of a unit of time for the given date.
moment().startOf('month');// => "2018-08-31T14:00:00.000Z"
// date-fnsimport startOfMonth from 'date-fns/startOfMonth';startOfMonth(new Date());// => "2018-08-31T14:00:00.000Z"
// dayjsdayjs().startOf('month');// => "2018-08-31T14:00:00.000Z"
// LuxonDateTime.local().startOf('month');// => "2018-09-02T09:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().with({day: 1});// => "2018-09-01T14:00:00.000Z"| Library | Time |
|---|---|
| Moment | 1078.948ms |
| Native | - |
| DateFns | 398.107ms |
| DayJs | 765.358ms |
| Luxon | 2306.765ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
End of Time
Section titled “End of Time”Return the end of a unit of time for the given date.
moment().endOf('day');// => "2018-09-09T13:59:59.999Z"
// Nativeconst end = new Date();end.setHours(23, 59, 59, 999);end.toISOString();// => "2018-09-09T16:59:59.999Z"
// date-fnsimport endOfDay from 'date-fns/endOfDay';endOfDay(new Date());// => "2018-09-09T13:59:59.999Z"
// dayjsdayjs().endOf('day');// => "2018-09-09T13:59:59.999Z"
// LuxonDateTime.local().endOf('day');// => "2018-09-02T09:12:49.695Z"
// TemporalTemporal.Now.zonedDateTimeISO().withPlainTime(new Temporal.PlainTime(23,59,59,999,999,999));// => "2018-09-09T16:59:59.999999999Z"| Library | Time |
|---|---|
| Moment | 1241.304ms |
| Native | 225.519ms |
| DateFns | 319.773ms |
| DayJs | 914.425ms |
| Luxon | 9920.529ms |
| Temporal | - |
[⬆ back to top]!(#quick-links)
Display
Section titled “Display”Format
Section titled “Format”Return the formatted date string in the given format.
moment().format('dddd, MMMM Do YYYY, h:mm:ss A');// => "Sunday, September 9th 2018, 7:12:49 PM"moment().format('ddd, hA');// => "Sun, 7PM"
// Nativenew Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'medium' }).format(new Date())// => "Sunday, September 9, 2018 at 7:12:49 PM"new Intl.DateTimeFormat('en-US', { weekday: 'short', hour: 'numeric' }).format(new Date())// => "Sun, 7 PM"
// date-fnsimport { intlFormat } from 'date-fns'intlFormat(new Date(), { dateStyle: 'full', timeStyle: 'medium' }, { locale: 'en-US', })// => "Sunday, September 9, 2018 at 7:12:49 PM"intlFormat(new Date(), { weekday: 'short', hour: 'numeric' }, { locale: 'en-US', })// => "Sun, 7 PM"
// dayjsdayjs().format('dddd, MMMM D YYYY, h:mm:ss A');// => "Sunday, September 9 2018, 7:12:49 PM"dayjs().format('ddd, hA');// => "Sun, 7PM"// dayjs ⚠️ requires advancedFormat plugin to support more format tokensimport advancedFormat from 'dayjs/plugin/advancedFormat';dayjs.extend(advancedFormat);dayjs().format('dddd, MMMM Do YYYY, h:mm:ss A');// => "Sunday, September 9th 2018, 7:12:49 PM"
// LuxonDateTime.fromMillis(time).toFormat('EEEE, MMMM dd yyyy, h:mm:ss a');// => "Sunday, September 9 2018, 7:12:49 PM" ⚠️ not support 9thDateTime.fromMillis(time).toFormat('EEE, ha');// => "Sun, 7PM"
// Temporalnew Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeStyle: 'medium' }).format(Temporal.Now.zonedDateTimeISO())// => "Sunday, September 9, 2018 at 7:12:49 PM"new Intl.DateTimeFormat('en-US', { weekday: 'short', hour: 'numeric' }).format(Temporal.Now.zonedDateTimeISO())// => "Sun, 7 PM"[⬆ back to top]!(#quick-links)
Time from now
Section titled “Time from now”Return time from now.
moment(1536484369695).fromNow();// => "4 days ago"
// Nativenew Intl.RelativeTimeFormat().format(-4, 'day');// => "4 days ago"
// date-fnsimport formatDistance from 'date-fns/formatDistance';formatDistance(new Date(1536484369695), new Date(), { addSuffix: true });// => "4 days ago"
// dayjs ⚠️ requires relativeTime pluginimport relativeTime from 'dayjs/plugin/relativeTime';dayjs.extend(relativeTime);
dayjs(1536484369695).fromNow();// => "5 days ago" ⚠️ the rounding method of this plugin is different from moment.js and date-fns, use with care.
// luxon requires Intl.RelativeTimeFormatDateTime.local(2022, 1, 27).toRelative({ base: this })// => "in 4 months"
// Temporalnew Intl.RelativeTimeFormat().format(-4, 'day');// => "4 days ago"[⬆ back to top]!(#quick-links)
Time from x
Section titled “Time from x”Return time from x.
moment([2007, 0, 27]).to(moment([2007, 0, 29]));// => "in 2 days"
// date-fnsimport formatDistance from 'date-fns/formatDistance';formatDistance(new Date(2007, 0, 27), new Date(2007, 0, 29));// => "2 days"
// dayjs ⚠️ requires relativeTime pluginimport relativeTime from 'dayjs/plugin/relativeTime';dayjs.extend(relativeTime);dayjs('2007-01-27').to(dayjs('2007-01-29'));// => "in 2 days"
// luxon ❌ does not support relative time
// TemporalTemporal.PlainDate.from('2007-01-27').until('2007-01-29');// => Temporal.Duration('P2D')[⬆ back to top]!(#quick-links)
Difference
Section titled “Difference”Get the unit of time between the given dates.
moment([2007, 0, 27]).diff(moment([2007, 0, 29]));// => -172800000moment([2007, 0, 27]).diff(moment([2007, 0, 29]), 'days');// => -2
// Nativenew Date(2007, 0, 27) - new Date(2007, 0, 29);// => -172800000Math.ceil( (new Date(2007, 0, 27) - new Date(2007, 0, 29)) / 1000 / 60 / 60 / 24);// => -2
// date-fnsimport differenceInMilliseconds from 'date-fns/differenceInMilliseconds';differenceInMilliseconds(new Date(2007, 0, 27), new Date(2007, 0, 29));// => -172800000import differenceInDays from 'date-fns/differenceInDays';differenceInDays(new Date(2007, 0, 27), new Date(2007, 0, 29));// => -2
// dayjsdayjs('2007-01-27').diff(dayjs('2007-01-29'), 'milliseconds');// => -172800000dayjs('2007-01-27').diff(dayjs('2007-01-29'), 'days');// => -2
// luxonDateTime.local(2007, 1, 27).diff(DateTime.local(2007, 1, 29)).milliseconds;// => -172800000DateTime.local(2007, 1, 27).diff(DateTime.local(2007, 1, 29), 'days').days;// => -2
// TemporalTemporal.PlainDate.from('2007-01-27').since('2007-01-29').total({unit: 'millisecond'});// => -172800000Temporal.PlainDate.from('2007-01-27').since('2007-01-29').total({unit: 'day'});// => -2[⬆ back to top]!(#quick-links)
Is Before
Section titled “Is Before”Check if a date is before another date.
moment('2010-10-20').isBefore('2010-10-21');// => true
// Nativenew Date(2010, 10, 20) < new Date(2010, 10, 21);// => true
// date-fnsimport isBefore from 'date-fns/isBefore';isBefore(new Date(2010, 9, 20), new Date(2010, 9, 21));// => true
// dayjsdayjs('2010-10-20').isBefore('2010-10-21');// => true
// luxonDateTime.fromISO('2010-10-20') < DateTime.fromISO('2010-10-21');// => true
// TemporalTemporal.PlainDate.compare('2010-10-20', '2010-10-21') === -1;// => true[⬆ back to top]!(#quick-links)
Is Same
Section titled “Is Same”Check if a date is the same as another date.
moment('2010-10-20').isSame('2010-10-21');// => falsemoment('2010-10-20').isSame('2010-10-20');// => truemoment('2010-10-20').isSame('2010-10-21', 'month');// => true
// Nativenew Date(2010, 9, 20).valueOf() === new Date(2010, 9, 21).valueOf();// => falsenew Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).valueOf();// => truenew Date(2010, 9, 20).getTime() === new Date(2010, 9, 20).getTime();// => truenew Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).getTime();// => truenew Date(2010, 9, 20).toDateString().substring(4, 7) === new Date(2010, 9, 21).toDateString().substring(4, 7);// => true
// date-fnsimport isSameDay from 'date-fns/isSameDay';import isSameMonth from 'date-fns/isSameMonth';isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 21));// => falseisSameDay(new Date(2010, 9, 20), new Date(2010, 9, 20));// => trueisSameMonth(new Date(2010, 9, 20), new Date(2010, 9, 21));// => true
// dayjsdayjs('2010-10-20').isSame('2010-10-21');// => falsedayjs('2010-10-20').isSame('2010-10-20');// => truedayjs('2010-10-20').isSame('2010-10-21', 'month');// => true
// luxon(+DateTime.fromISO('2010-10-20') === +DateTime.fromISO('2010-10-21') + // => false DateTime.fromISO('2010-10-20')) === +DateTime.fromISO('2010-10-20');// => trueDateTime.fromISO('2010-10-20').hasSame(DateTime.fromISO('2010-10-21'), 'month');// => true
// TemporalTemporal.PlainDate.from('2010-10-20').equals('2010-10-21');// => falseTemporal.PlainDate.from('2010-10-20').equals('2010-10-20');// => trueTemporal.PlainDate.from('2010-10-20').month === Temporal.PlainDate.from('2010-10-21').month;// => true[⬆ back to top]!(#quick-links)
Is After
Section titled “Is After”Check if a date is after another date.
moment('2010-10-20').isAfter('2010-10-19');// => true
// Nativenew Date(2010, 9, 20) > new Date(2010, 9, 19);// => true
// date-fnsimport isAfter from 'date-fns/isAfter';isAfter(new Date(2010, 9, 20), new Date(2010, 9, 19));// => true
// dayjsdayjs('2010-10-20').isAfter('2010-10-19');// => true
// luxonDateTime.fromISO('2010-10-20') > DateTime.fromISO('2010-10-19');// => true
// TemporalTemporal.PlainDate.compare('2010-10-20', '2010-10-19') === 1;// => true[⬆ back to top]!(#quick-links)
Is Between
Section titled “Is Between”Check if a date is between two other dates.
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25');// => true
// date-fnsimport isWithinInterval from 'date-fns/isWithinInterval';isWithinInterval(new Date(2010, 9, 20), { start: new Date(2010, 9, 19), end: new Date(2010, 9, 25),});// => true
// dayjs ⚠️ requires isBetween pluginimport isBetween from 'dayjs/plugin/isBetween';dayjs.extend(isBetween);dayjs('2010-10-20').isBetween('2010-10-19', '2010-10-25');// => true
// luxonInterval.fromDateTimes( DateTime.fromISO('2010-10-19'), DateTime.fromISO('2010-10-25')).contains(DateTime.fromISO('2010-10-20'));// => true[⬆ back to top]!(#quick-links)
Is Leap Year
Section titled “Is Leap Year”Check if a year is a leap year.
moment([2000]).isLeapYear();// => true
// Nativenew Date(2000, 1, 29).getDate() === 29;// => true
// date-fnsimport isLeapYear from 'date-fns/isLeapYear';isLeapYear(new Date(2000, 0, 1));// => true
// dayjs ⚠️ requires isLeapYear pluginimport isLeapYear from 'dayjs/plugin/isLeapYear';dayjs.extend(isLeapYear);dayjs('2000-01-01').isLeapYear();// => true
// luxonexpect(DateTime.local(2000).isInLeapYear).toBeTruthy();// => true
// TemporalTemporal.PlainDate.from('2000-01-01').inLeapYear;// => true[⬆ back to top]!(#quick-links)
Is a Date
Section titled “Is a Date”Check if a variable is a native js Date object.
moment.isDate(new Date());// => true
// Nativenew Date() instanceof Date;// => true
// date-fnsimport isDate from 'date-fns/isDate';isDate(new Date());// => true
// dayjsdayjs(new Date()).isValid();
// luxonDateTime.local().isValid;// => true
// Temporalnew Date() instanceof Date;Temporal.Now.plainTimeISO() instanceof Temporal.PlainTime;Temporal.Now.plainDateISO() instanceof Temporal.PlainDate;Temporal.Now.plainDateTimeISO() instanceof Temporal.PlainDateTime;Temporal.Now.zonedDateTimeISO() instanceof Temporal.ZonedDateTime;// => true[⬆ back to top]!(#quick-links)
License
Section titled “License”MIT