Gathering detailed insights and metrics for eslint-plugin-you-dont-need-momentjs
Gathering detailed insights and metrics for eslint-plugin-you-dont-need-momentjs
Gathering detailed insights and metrics for eslint-plugin-you-dont-need-momentjs
Gathering detailed insights and metrics for eslint-plugin-you-dont-need-momentjs
npm install eslint-plugin-you-dont-need-momentjs
Typescript
Module System
Node Version
NPM Version
56.6
Supply Chain
94
Quality
75.9
Maintenance
100
Vulnerability
97
License
JavaScript (100%)
Total Downloads
3,319,572
Last Day
2,128
Last Week
9,342
Last Month
104,391
Last Year
963,650
13,289 Stars
290 Commits
317 Forks
140 Watching
3 Branches
42 Contributors
Latest Version
1.6.0
Package Id
eslint-plugin-you-dont-need-momentjs@1.6.0
Size
114.07 kB
NPM Version
6.13.4
Node Version
12.14.1
Publised On
04 Mar 2020
Cumulative downloads
Total Downloads
Last day
-60.2%
2,128
Compared to previous day
Last week
-67.7%
9,342
Compared to previous week
Last month
-17.8%
104,391
Compared to previous month
Last year
80%
963,650
Compared to previous year
1
1
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.
Problems with Moment.js:
a.subtract('ms', 50)
, a.subtract(50, 'ms')
and even a.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.
Name | Size original/gzipped | Tree-shaking | Popularity (stars) | Methods richness | Pattern | Timezone Support | Locale |
---|---|---|---|---|---|---|---|
Moment.js | 329K/69.6K | No | 43.4k | High | OO | Good (moment-timezone) | 123 |
Luxon | 59.9K/17.2K | No | 9k | High | OO | Good (Intl) | - |
date-fns | 78.4k/13.4k without tree-shaking | Yes | 21.3k | High | Functional | Good (date-fns-tz) | 64 |
dayjs | 6.5k/2.6k without plugins | No | 25.8k | High | OO | Not yet | 130 |
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.
—Sergey Petushkov, a javaScript developer from Moscow, Russia • Currently in Berlin, Germany.
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...
1npm install --save-dev eslint-plugin-you-dont-need-momentjs
...then update your config
1"extends" : ["plugin:you-dont-need-momentjs/recommended"],
⚠️ Indicates other packages or work are needed. See individual functions above.
Native | Luxon | date-fns | dayjs | |
---|---|---|---|---|
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 | ✅ | ✅ | ✅ | ✅ |
Return the date parsed from date string using the given format string.
1// Moment.js 2moment('12-25-1995', 'MM-DD-YYYY'); 3// => "1995-12-24T13:00:00.000Z" 4 5// Native 6const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/; 7const [, month, day, year] = datePattern.exec('12-25-1995'); 8new Date(`${month}, ${day} ${year}`); 9// => "1995-12-24T13:00:00.000Z" 10 11// date-fns 12import parse from 'date-fns/parse'; 13parse('12-25-1995', 'MM-dd-yyyy', new Date()); 14// => "1995-12-24T13:00:00.000Z" 15 16// dayjs 17dayjs('12-25-1995'); 18// => "1995-12-24T13:00:00.000Z" 19 20// luxon 21DateTime.fromFormat('12-25-1995', 'MM-dd-yyyy').toJSDate(); 22// => "1995-12-24T13:00:00.000Z"
Return the date parsed from time string using the given format string.
1// Moment.js 2moment('2010-10-20 4:30', 'YYYY-MM-DD HH:mm'); 3// => "2010-10-19T17:30:00.000Z" 4 5// Native 6const datePattern = /^(\d{4})-(\d{2})-(\d{2})\s(\d{1,2}):(\d{2})$/; 7const [, year, month, day, rawHour, min] = datePattern.exec('2010-10-20 4:30'); 8new Date(`${year}-${month}-${day}T${('0' + rawHour).slice(-2)}:${min}:00`); 9// => "2010-10-19T17:30:00.000Z" 10 11// date-fns 12import parse from 'date-fns/parse'; 13parse('2010-10-20 4:30', 'yyyy-MM-dd H:mm', new Date()); 14// => "2010-10-19T17:30:00.000Z" 15 16// dayjs ⚠️ requires customParseFormat plugin 17import customParseFormat from 'dayjs/plugin/customParseFormat'; 18dayjs.extend(customParseFormat); 19dayjs('2010-10-20 4:30', 'YYYY-MM-DD HH:mm'); 20// => "2010-10-19T17:30:00.000Z" 21 22// luxon 23DateTime.fromFormat('2010-10-20 4:30', 'yyyy-MM-dd H:mm').toJSDate(); 24// => "2010-10-19T17:30:00.000Z"
Return the date parsed from string using the given format string and locale.
1// Moment.js 2moment('2012 mars', 'YYYY MMM', 'fr'); 3// => "2012-02-29T13:00:00.000Z" 4 5// date-fns 6import parse from 'date-fns/parse'; 7import fr from 'date-fns/locale/fr'; 8parse('2012 mars', 'yyyy MMMM', new Date(), { locale: fr }); 9// => "2012-02-29T13:00:00.000Z" 10 11// dayjs ⚠️ requires customParseFormat plugin 12import customParseFormat from 'dayjs/plugin/customParseFormat'; 13import 'dayjs/locale/fr'; 14dayjs.extend(customParseFormat); 15dayjs('2012 mars', 'YYYY MMM', 'fr'); 16// => "2012-02-29T13:00:00.000Z" 17 18// Luxon ❌ does not support Locale for node unless https://moment.github.io/luxon/docs/manual/install.html#node 19DateTime.fromFormat('2012 mars', 'yyyy MMMM', { locale: 'fr' }); 20// => "2012-02-29T13:00:00.000Z"
Get the Millisecond/Second/Minute/Hour
of the given date.
1// Moment.js 2moment().seconds(); 3// => 49 4moment().hours(); 5// => 19 6 7// Native 8new Date().getSeconds(); 9// => 49 10new Date().getHours(); 11// => 19 12 13// date-fns 14import getSeconds from 'date-fns/getSeconds'; 15import getHours from 'date-fns/getHours'; 16getSeconds(new Date()); 17// => 49 18getHours(new Date()); 19// => 19 20 21// dayjs 22dayjs().second(); 23// => 49 24dayjs().hour(); 25// => 19 26 27// Luxon 28DateTime.local().second; 29// => 49 30DateTime.local().hour; 31// => 19
Library | Time |
---|---|
Moment | 1500.703ms |
Native | 348.411ms |
DateFns | 520.670ms |
DayJs | 494.234ms |
Luxon | 1208.368ms |
Set the Millisecond/Second/Minute/Hour
of the given date.
1// Moment.js 2moment().seconds(30); 3// => "2018-09-09T09:12:30.695Z" 4moment().hours(13); 5// => "2018-09-09T03:12:49.695Z" 6 7// Native 8new Date(new Date().setSeconds(30)); 9// => "2018-09-09T09:12:30.695Z" 10new Date(new Date().setHours(13)); 11// => "2018-09-09T03:12:49.695Z" 12 13// date-fns 14import setSeconds from 'date-fns/setSeconds'; 15import setHours from 'date-fns/setHours'; 16setSeconds(new Date(), 30); 17// => "2018-09-09T09:12:30.695Z" 18setHours(new Date(), 13); 19// => "2018-09-09T03:12:49.695Z" 20 21// dayjs 22dayjs().set('second', 30); 23// => "2018-09-09T09:12:30.695Z" 24dayjs().set('hour', 13); 25// => "2018-09-09T03:12:49.695Z" 26 27// luxon 28DateTime.utc() 29 .set({ second: 30 }) 30 .toJSDate(); 31// => "2018-09-09T09:12:30.695Z" 32DateTime.utc() 33 .set({ hour: 13 }) 34 .toJSDate(); 35// => "2018-09-09T03:12:49.695Z"
Library | Time |
---|---|
Moment | 1689.744ms |
Native | 636.741ms |
DateFns | 714.148ms |
DayJs | 2037.603ms |
Luxon | 2897.571ms |
Gets or sets the day of the month.
1// Moment.js 2moment().date(); 3// => 9 4moment().date(4); 5// => "2018-09-04T09:12:49.695Z" 6 7// Native 8new Date().getDate(); 9// => 9 10new Date().setDate(4); 11// => "2018-09-04T09:12:49.695Z" 12 13// date-fns 14import getDate from 'date-fns/getDate'; 15import setDate from 'date-fns/setDate'; 16getDate(new Date()); 17// => 9 18setDate(new Date(), 4); 19// => "2018-09-04T09:12:49.695Z" 20 21// dayjs 22dayjs().date(); 23// => 9 24dayjs().set('date', 4); 25// => "2018-09-04T09:12:49.695Z" 26 27// luxon 28DateTime.utc().day; 29// => 9 30DateTime.utc() 31 .set({ day: 4 }) 32 .toString(); 33// => "2018-09-04T09:12:49.695Z"
Library | Time |
---|---|
Moment | 1381.669ms |
Native | 397.415ms |
DateFns | 588.004ms |
DayJs | 1218.025ms |
Luxon | 2705.606ms |
Gets or sets the day of the week.
1// Moment.js 2moment().day(); 3// => 0 (Sunday) 4moment().day(-14); 5// => "2018-08-26T09:12:49.695Z" 6 7// Native 8new Date().getDay(); 9// => 0 (Sunday) 10new Date().setDate(new Date().getDate() - 14); 11// => "2018-08-26T09:12:49.695Z" 12 13// date-fns 14import getDay from 'date-fns/getDay'; 15import setDay from 'date-fns/setDay'; 16getDay(new Date()); 17// => 0 (Sunday) 18setDay(new Date(), -14); 19// => "2018-08-26T09:12:49.695Z" 20 21// dayjs 22dayjs().day(); 23// => 0 (Sunday) 24dayjs().set('day', -14); 25// => "2018-08-26T09:12:49.695Z" 26 27// Luxon 28DateTime.local().weekday; 29// => 7 (Sunday) 30DateTime.local() 31 .minus({ day: 14 }) 32 .toJSDate(); 33// => "2018-08-26T09:12:49.695Z"
Library | Time |
---|---|
Moment | 1919.404ms |
Native | 543.466ms |
DateFns | 841.436ms |
DayJs | 1229.475ms |
Luxon | 3936.282ms |
Gets or sets the day of the year.
1// Moment.js 2moment().dayOfYear(); 3// => 252 4moment().dayOfYear(256); 5// => "2018-09-13T09:12:49.695Z" 6 7// Native 8Math.floor( 9 (new Date() - new Date(new Date().getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24 10); 11// => 252 12 13// date-fns 14import getDayOfYear from 'date-fns/getDayOfYear'; 15import setDayOfYear from 'date-fns/setDayOfYear'; 16getDayOfYear(new Date()); 17// => 252 18setDayOfYear(new Date(), 256); 19// => "2018-09-13T09:12:49.695Z" 20 21// dayjs ⚠️ requires dayOfYear plugin 22import dayOfYear from 'dayjs/plugin/dayOfYear'; 23dayjs.extend(dayOfYear); 24dayjs().dayOfYear(); 25// => 252 26dayjs().dayOfYear(256); 27// => "2018-09-13T09:12:49.695Z" 28 29// Luxon 30DateTime.local().ordinal; 31// => 252 32DateTime.local() 33 .set({ ordinal: 256 }) 34 .toString(); 35// => "2018-09-13T09:12:49.695Z"
Library | Time |
---|---|
Moment | 5511.172ms |
Native | 530.592ms |
DateFns | 2079.043ms |
DayJs | - |
Luxon | 3540.810ms |
Gets or sets the week of the year.
1// Moment.js 2moment().week(); 3// => 37 4moment().week(24); 5// => "2018-06-10T09:12:49.695Z" 6 7// date-fns 8import getWeek from 'date-fns/getWeek'; 9import setWeek from 'date-fns/setWeek'; 10getWeek(new Date()); 11// => 37 12setWeek(new Date(), 24); 13// => "2018-06-10T09:12:49.695Z" 14 15// native getWeek 16const day = new Date(); 17const MILLISECONDS_IN_WEEK = 604800000; 18const firstDayOfWeek = 1; // monday as the first day (0 = sunday) 19const startOfYear = new Date(day.getFullYear(), 0, 1); 20startOfYear.setDate( 21 startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7)) 22); 23const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1; 24// => 37 25 26// native setWeek 27const day = new Date(); 28const week = 24; 29const MILLISECONDS_IN_WEEK = 604800000; 30const firstDayOfWeek = 1; // monday as the first day (0 = sunday) 31const startOfYear = new Date(day.getFullYear(), 0, 1); 32startOfYear.setDate( 33 startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7)) 34); 35const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1; 36day.setDate(day.getDate() - (dayWeek - week) * 7); 37day.toISOString(); 38// => "2018-06-10T09:12:49.794Z 39 40// dayjs ⚠️ requires weekOfYear plugin 41import weekOfYear from 'dayjs/plugin/weekOfYear'; 42dayjs.extend(weekOfYear); 43dayjs().week(); 44// => 37 45dayjs().week(24); 46// => "2018-06-10T09:12:49.695Z" 47 48// Luxon 49DateTime.local().weekNumber; 50// => 37 51DateTime.local() 52 .set({ weekNumber: 23 }) 53 .toString(); 54// => "2018-06-10T09:12:49.794Z
Library | Time |
---|---|
Moment | 7147.201ms |
Native | 1371.631ms |
DateFns | 5834.815ms |
DayJs | - |
Luxon | 4514.771ms |
Get the number of days in the current month.
1// Moment.js 2moment('2012-02', 'YYYY-MM').daysInMonth(); 3// => 29 4 5// Native 6new Date(2012, 02, 0).getDate(); 7// => 29 8 9// date-fns 10import getDaysInMonth from 'date-fns/getDaysInMonth'; 11getDaysInMonth(new Date(2012, 1)); 12// => 29 13 14// dayjs 15dayjs('2012-02').daysInMonth(); 16// => 29 17 18// Luxon 19DateTime.local(2012, 2).daysInMonth; 20// => 29
Library | Time |
---|---|
Moment | 4415.065ms |
Native | 186.196ms |
DateFns | 634.084ms |
DayJs | 1922.774ms |
Luxon | 1403.032ms |
Gets the number of weeks in the current year, according to ISO weeks.
1// Moment.js 2moment().isoWeeksInYear(); 3// => 52 4 5// date-fns 6import getISOWeeksInYear from 'date-fns/getISOWeeksInYear'; 7getISOWeeksInYear(new Date()); 8// => 52 9 10// dayjs ⚠️ requires isoWeeksInYear plugin 11import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear'; 12dayjs.extend(isoWeeksInYear); 13dayjs().isoWeeksInYear(); 14// => 52 15 16// Moment.js 17DateTime.local().weeksInWeekYear; 18// => 52
Library | Time |
---|---|
Moment | 1065.247ms |
Native | - |
DateFns | 4954.042ms |
DayJs | - |
Luxon | 1134.483ms |
Returns the maximum (most distant future) of the given date.
1const array = [ 2 new Date(2017, 4, 13), 3 new Date(2018, 2, 12), 4 new Date(2016, 0, 10), 5 new Date(2016, 0, 9), 6]; 7// Moment.js 8moment.max(array.map(a => moment(a))); 9// => "2018-03-11T13:00:00.000Z" 10 11// Native 12new Date(Math.max.apply(null, array)).toISOString(); 13// => "2018-03-11T13:00:00.000Z" 14 15// date-fns 16import max from 'date-fns/max'; 17max(array); 18// => "2018-03-11T13:00:00.000Z" 19 20// dayjs ⚠️ requires minMax plugin 21import minMax from 'dayjs/plugin/minMax'; 22dayjs.extend(minMax); 23dayjs.max(array.map(a => dayjs(a))); 24// => "2018-03-11T13:00:00.000Z" 25 26// Luxon 27DateTime.max(...array.map(a => DateTime.fromJSDate(a))).toJSDate(); 28// => "2018-03-11T13:00:00.000Z"
Library | Time |
---|---|
Moment | 1780.075ms |
Native | 828.332ms |
DateFns | 980.938ms |
DayJs | - |
Luxon | 2694.702ms |
Returns the minimum (most distant future) of the given date.
1const array = [ 2 new Date(2017, 4, 13), 3 new Date(2018, 2, 12), 4 new Date(2016, 0, 10), 5 new Date(2016, 0, 9), 6]; 7// Moment.js 8moment.min(array.map(a => moment(a))); 9// => "2016-01-08T13:00:00.000Z" 10 11// Native 12new Date(Math.min.apply(null, array)).toISOString(); 13// => "2016-01-08T13:00:00.000Z" 14 15// date-fns 16import min from 'date-fns/min'; 17min(array); 18// => "2016-01-08T13:00:00.000Z" 19 20// dayjs ⚠️ requires minMax plugin 21import minMax from 'dayjs/plugin/minMax'; 22dayjs.extend(minMax); 23dayjs.min(array.map(a => dayjs(a))); 24// => "2016-01-08T13:00:00.000Z" 25 26// Luxon 27DateTime.min(...array.map(a => DateTime.fromJSDate(a))).toJSDate(); 28// => "2016-01-08T13:00:00.000Z"
Library | Time |
---|---|
Moment | 1744.459ms |
Native | 819.646ms |
DateFns | 841.249ms |
DayJs | - |
Luxon | 2720.462ms |
Add the specified number of days to the given date.
1// Moment.js 2moment().add(7, 'days'); 3// => "2018-09-16T09:12:49.695Z" 4 5// Native 6const now = new Date(); 7now.setDate(now.getDate() + 7); 8// => "Sun Sep 16 2018 09:12:49" 9 10// date-fns 11import addDays from 'date-fns/addDays'; 12addDays(new Date(), 7); 13// => "2018-09-16T09:12:49.695Z" 14 15// dayjs 16dayjs().add(7, 'day'); 17// => "2018-09-16T09:12:49.695Z" 18 19// Luxon 20DateTime.local() 21 .plus({ day: 7 }) 22 .toJSDate(); 23// => "2018-09-16T09:12:49.695Z"
Library | Time |
---|---|
Moment | 1309.485ms |
Native | 259.932ms |
DateFns | 385.394ms |
DayJs | 1911.881ms |
Luxon | 3919.797ms |
Subtract the specified number of days from the given date.
1// Moment.js 2moment().subtract(7, 'days'); 3// => "2018-09-02T09:12:49.695Z" 4 5// Native 6new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7); 7// => Sun Sep 09 2018 09:12:49 8 9// date-fns 10import subDays from 'date-fns/subDays'; 11subDays(new Date(), 7); 12// => "2018-09-02T09:12:49.695Z" 13 14// dayjs 15dayjs().subtract(7, 'day'); 16// => "2018-09-02T09:12:49.695Z" 17 18// Luxon 19DateTime.local() 20 .minus({ day: 7 }) 21 .toJSDate(); 22// => "2018-09-02T09:12:49.695Z"
Library | Time |
---|---|
Moment | 1278.384ms |
Native | 215.255ms |
DateFns | 379.057ms |
DayJs | 1772.593ms |
Luxon | 4028.866ms |
Return the start of a unit of time for the given date.
1// Moment.js 2moment().startOf('month'); 3// => "2018-08-31T14:00:00.000Z" 4 5// date-fns 6import startOfMonth from 'date-fns/startOfMonth'; 7startOfMonth(new Date()); 8// => "2018-08-31T14:00:00.000Z" 9 10// dayjs 11dayjs().startOf('month'); 12// => "2018-08-31T14:00:00.000Z" 13 14// Luxon 15DateTime.local().startOf('month'); 16// => "2018-09-02T09:12:49.695Z"
Library | Time |
---|---|
Moment | 1078.948ms |
Native | - |
DateFns | 398.107ms |
DayJs | 765.358ms |
Luxon | 2306.765ms |
Return the end of a unit of time for the given date.
1// Moment.js 2moment().endOf('day'); 3// => "2018-09-09T13:59:59.999Z" 4 5// Native 6const end = new Date(); 7end.setHours(23, 59, 59, 999); 8end.toISOString(); 9// => "2018-09-09T16:59:59.999Z" 10 11// date-fns 12import endOfDay from 'date-fns/endOfDay'; 13endOfDay(new Date()); 14// => "2018-09-09T13:59:59.999Z" 15 16// dayjs 17dayjs().endOf('day'); 18// => "2018-09-09T13:59:59.999Z" 19 20// Luxon 21DateTime.local().endOf('day'); 22// => "2018-09-02T09:12:49.695Z"
Library | Time |
---|---|
Moment | 1241.304ms |
Native | 225.519ms |
DateFns | 319.773ms |
DayJs | 914.425ms |
Luxon | 9920.529ms |
Return the formatted date string in the given format.
1// Moment.js 2moment().format('dddd, MMMM Do YYYY, h:mm:ss A'); 3// => "Sunday, September 9th 2018, 7:12:49 PM" 4moment().format('ddd, hA'); 5// => "Sun, 7PM" 6 7// date-fns 8import format from 'date-fns/format'; 9format(new Date(), 'eeee, MMMM do YYYY, h:mm:ss aa'); 10// => "Sunday, September 9th 2018, 7:12:49 PM" 11format(new Date(), 'eee, ha'); 12// => "Sun, 7PM" 13 14// dayjs 15dayjs().format('dddd, MMMM D YYYY, h:mm:ss A'); 16// => "Sunday, September 9 2018, 7:12:49 PM" 17dayjs().format('ddd, hA'); 18// => "Sun, 7PM" 19// dayjs ⚠️ requires advancedFormat plugin to support more format tokens 20import advancedFormat from 'dayjs/plugin/customParseFormat'; 21dayjs.extend(advancedFormat); 22dayjs().format('dddd, MMMM Do YYYY, h:mm:ss A'); 23// => "Sunday, September 9th 2018, 7:12:49 PM" 24 25// Luxon 26DateTime.fromMillis(time).toFormat('EEEE, MMMM dd yyyy, h:mm:ss a'); 27// => "Sunday, September 9 2018, 7:12:49 PM" ⚠️ not support 9th 28DateTime.fromMillis(time).toFormat('EEE, ha'); 29// => "Sun, 7PM"
Return time from now.
1// Moment.js 2moment(1536484369695).fromNow(); 3// => "4 days ago" 4 5// date-fns 6import formatDistance from 'date-fns/formatDistance'; 7formatDistance(new Date(1536484369695), new Date(), { addSuffix: true }); 8// => "4 days ago" 9 10// dayjs ⚠️ requires relativeTime plugin 11import relativeTime from 'dayjs/plugin/relativeTime'; 12dayjs.extend(relativeTime); 13 14dayjs(1536484369695).fromNow(); 15// => "5 days ago" ⚠️ the rounding method of this plugin is different from moment.js and date-fns, use with care. 16 17// luxon ❌ does not support relative time
Return time from x.
1// Moment.js 2moment([2007, 0, 27]).to(moment([2007, 0, 29])); 3// => "in 2 days" 4 5// date-fns 6import formatDistance from 'date-fns/formatDistance'; 7formatDistance(new Date(2007, 0, 27), new Date(2007, 0, 29)); 8// => "2 days" 9 10// dayjs ⚠️ requires relativeTime plugin 11import relativeTime from 'dayjs/plugin/relativeTime'; 12dayjs.extend(relativeTime); 13dayjs('2007-01-27').to(dayjs('2007-01-29')); 14// => "in 2 days" 15 16// luxon ❌ does not support relative time
Get the unit of time between the given dates.
1// Moment.js 2moment([2007, 0, 27]).diff(moment([2007, 0, 29])); 3// => -172800000 4moment([2007, 0, 27]).diff(moment([2007, 0, 29]), 'days'); 5// => -2 6 7// Native 8new Date(2007, 0, 27) - new Date(2007, 0, 29); 9// => -172800000 10Math.ceil( 11 (new Date(2007, 0, 27) - new Date(2007, 0, 29)) / 1000 / 60 / 60 / 24 12); 13// => -2 14 15// date-fns 16import differenceInMilliseconds from 'date-fns/differenceInMilliseconds'; 17differenceInMilliseconds(new Date(2007, 0, 27), new Date(2007, 0, 29)); 18// => -172800000 19import differenceInDays from 'date-fns/differenceInDays'; 20differenceInDays(new Date(2007, 0, 27), new Date(2007, 0, 29)); 21// => -2 22 23// dayjs 24dayjs('2007-01-27').diff(dayjs('2007-01-29'), 'milliseconds'); 25// => -172800000 26dayjs('2007-01-27').diff(dayjs('2007-01-29'), 'days'); 27// => -2 28 29// luxon 30DateTime.local(2007, 1, 27).diff(DateTime.local(2007, 1, 29)).milliseconds; 31// => -172800000 32DateTime.local(2007, 1, 27).diff(DateTime.local(2007, 1, 29), 'days').days; 33// => -2
Check if a date is before another date.
1// Moment.js 2moment('2010-10-20').isBefore('2010-10-21'); 3// => true 4 5// Native 6new Date(2010, 10, 20) < new Date(2010, 10, 21); 7// => true 8 9// date-fns 10import isBefore from 'date-fns/isBefore'; 11isBefore(new Date(2010, 9, 20), new Date(2010, 9, 21)); 12// => true 13 14// dayjs 15dayjs('2010-10-20').isBefore('2010-10-21'); 16// => true 17 18// luxon 19DateTime.fromISO('2010-10-20') < DateTime.fromISO('2010-10-21'); 20// => true
Check if a date is the same as another date.
1// Moment.js 2moment('2010-10-20').isSame('2010-10-21'); 3// => false 4moment('2010-10-20').isSame('2010-10-20'); 5// => true 6moment('2010-10-20').isSame('2010-10-21', 'month'); 7// => true 8 9// Native 10new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 21).valueOf(); 11// => false 12new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).valueOf(); 13// => true 14new Date(2010, 9, 20).getTime() === new Date(2010, 9, 20).getTime(); 15// => true 16new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).getTime(); 17// => true 18new Date(2010, 9, 20).toDateString().substring(4, 7) === 19 new Date(2010, 9, 21).toDateString().substring(4, 7); 20// => true 21 22// date-fns 23import isSameDay from 'date-fns/isSameDay'; 24import isSameMonth from 'date-fns/isSameMonth'; 25isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 21)); 26// => false 27isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 20)); 28// => true 29isSameMonth(new Date(2010, 9, 20), new Date(2010, 9, 21)); 30// => true 31 32// dayjs 33dayjs('2010-10-20').isSame('2010-10-21'); 34// => false 35dayjs('2010-10-20').isSame('2010-10-20'); 36// => true 37dayjs('2010-10-20').isSame('2010-10-21', 'month'); 38// => true 39 40// luxon 41(+DateTime.fromISO('2010-10-20') === 42 +DateTime.fromISO('2010-10-21') + 43 // => false 44 DateTime.fromISO('2010-10-20')) === 45 +DateTime.fromISO('2010-10-20'); 46// => true 47DateTime.fromISO('2010-10-20').hasSame(DateTime.fromISO('2010-10-21'), 'month'); 48// => true
Check if a date is after another date.
1// Moment.js 2moment('2010-10-20').isAfter('2010-10-19'); 3// => true 4 5// Native 6new Date(2010, 9, 20) > new Date(2010, 9, 19); 7// => true 8 9// date-fns 10import isAfter from 'date-fns/isAfter'; 11isAfter(new Date(2010, 9, 20), new Date(2010, 9, 19)); 12// => true 13 14// dayjs 15dayjs('2010-10-20').isAfter('2010-10-19'); 16// => true 17 18// luxon 19DateTime.fromISO('2010-10-20') > DateTime.fromISO('2010-10-19'); 20// => true
Check if a date is between two other dates.
1// Moment.js 2moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); 3// => true 4 5// date-fns 6import isWithinInterval from 'date-fns/isWithinInterval'; 7isWithinInterval(new Date(2010, 9, 20), { 8 start: new Date(2010, 9, 19), 9 end: new Date(2010, 9, 25), 10}); 11// => true 12 13// dayjs ⚠️ requires isBetween plugin 14import isBetween from 'dayjs/plugin/isBetween'; 15dayjs.extend(isBetween); 16dayjs('2010-10-20').isBetween('2010-10-19', '2010-10-25'); 17// => true 18 19// luxon 20Interval.fromDateTimes( 21 DateTime.fromISO('2010-10-19'), 22 DateTime.fromISO('2010-10-25') 23).contains(DateTime.fromISO('2010-10-20')); 24// => true
Check if a year is a leap year.
1// Moment.js 2moment([2000]).isLeapYear(); 3// => true 4 5// Native 6new Date(2000, 1, 29).getDate() === 29; 7// => true 8 9// date-fns 10import isLeapYear from 'date-fns/isLeapYear'; 11isLeapYear(new Date(2000, 0, 1)); 12// => true 13 14// dayjs ⚠️ requires isLeapYear plugin 15import isLeapYear from 'dayjs/plugin/isLeapYear'; 16dayjs.extend(isLeapYear); 17dayjs('2000-01-01').isLeapYear(); 18// => true 19 20// luxon 21expect(DateTime.local(2000).isInLeapYear).toBeTruthy(); 22// => true
Check if a variable is a native js Date object.
1// Moment.js 2moment.isDate(new Date()); 3// => true 4 5// Native 6new Date() instanceof Date; 7// => true 8 9// date-fns 10import isDate from 'date-fns/isDate'; 11isDate(new Date()); 12// => true 13 14// dayjs 15dayjs(new Date()).isValid(); 16 17// luxon 18DateTime.local().isValid; 19// => true
MIT
No vulnerabilities found.
Reason
all changesets reviewed
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
8 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More