You don't (may not) need Moment.js
Moment.js is a fantastic date library with lots of great features and utilities. However, when you are working on a performance sensitive web application, it might have a huge performance overhead because of the complex API and large bundle size.
Problems with Moment.js:
Only use some simple functions from moment.js might be considered overkill, date-fns can be a good replacement in this case. see https://github.com/moment/moment/issues/2373 for more ideas of why and how people switch from moment.js to date-fns.
Voice of Developers
Removed moment.js to replace with date-fns - build output reduced by 40%
—Jared Farago from webnode project.
Make use of native JavaScript object and array utilities before going big.Good library if you’re looking to replace Moment.js for one reason or another. Immutable too.
—Dan Abramov, Author of Co-author of Redux and Create React App. Building tools for humans.
I strongly recommend using date-fns over Moment.js, it's has a nicer API and you can include only parts you need!
—Matija Marohnić, a design-savvy frontend developer from Croatia.
Just yesterday changed momentjs to this lib in out project. Cut the size of our js bundle almost in half 😱
—Sergey Petushkov, a javaScript developer from Moscow, Russia • Currently in Berlin, Germany.
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
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
⚠️ Note that the version of date-fns used in this example is v2, See v1 docs
Parse
String + Date Format
Return the date parsed from date string using the given format string.
moment("12-25-1995", "MM-DD-YYYY");
import parse from "date-fns/parse";
parse("12-25-1995", "MM-dd-yyyy", new Date());
⬆ back to top
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");
import parse from "date-fns/parse";
parse("2010-10-20 4:30", "yyyy-MM-dd H:mm", new Date());
⬆ back to top
String + Format + locale
Return the date parsed from string using the given format string and locale.
moment("2012 mars", "YYYY MMM", "fr");
import parse from "date-fns/parse";
import fr from "date-fns/locale/fr";
parse("2012 mars", "yyyy MMMM", new Date(), { locale: fr });
⬆ back to top
Get + Set
Millisecond / Second / Minute / Hour
Get the Millisecond/Second/Minute/Hour
of the given date.
moment().seconds();
moment().hours();
new Date().getSeconds();
new Date().getHours();
Set the Millisecond/Second/Minute/Hour
of the given date.
moment().seconds(30);
moment().hours(13);
new Date(new Date().setSeconds(30));
new Date(new Date().getHours(13));
⬆ back to top
Date of Month
Gets or sets the day of the month.
moment().date();
moment().date(4);
new Date().getDate();
new Date().setDate(4);
⬆ back to top
Day of Week
Gets or sets the day of the week.
moment().day();
moment().day(-14);
new Date().getDay();
new Date().setDate(new Date().getDate() - 14);
⬆ back to top
Day of Year
Gets or sets the day of the year.
moment().dayOfYear();
moment().dayOfYear(256);
import getDayOfYear from "date-fns/getDayOfYear";
getDayOfYear(new Date());
import setDayOfYear from "date-fns/setDayOfYear";
setDayOfYear(new Date(), 256);
⬆ back to top
Week of Year
Gets or sets the week of the year.
moment().week();
moment().week(24);
import getWeek from "date-fns/getWeek";
getWeek(new Date());
import setWeek from "date-fns/setWeek";
setWeek(new Date(), 24);
⬆ back to top
Days in Month
Get the number of days in the current month.
moment("2012-02", "YYYY-MM").daysInMonth();
import getDaysInMonth from "date-fns/getDaysInMonth";
getDaysInMonth(new Date(2012, 1));
⬆ back to top
Weeks in Year
Gets the number of weeks in the current year, according to ISO weeks.
moment().isoWeeksInYear();
import getISOWeeksInYear from "date-fns/getISOWeeksInYear";
getISOWeeksInYear(new Date());
⬆ back to top
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.max(array.map(a => moment(a)));
import max from "date-fns/max";
max(array);
⬆ back to top
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.min(array.map(a => moment(a)));
import min from "date-fns/min";
min(array);
⬆ back to top
Manipulate
Add
Add the specified number of days to the given date.
moment().add(7, "days");
import addDays from "date-fns/addDays";
addDays(new Date(), 7);
⬆ back to top
Subtract
Subtract the specified number of days from the given date.
moment().subtract(7, "days");
import subDays from "date-fns/subDays";
subDays(new Date(), 7);
⬆ back to top
Start of Time
Return the start of a unit of time for the given date.
moment().startOf("month");
import startOfMonth from "date-fns/startOfMonth";
startOfMonth(new Date());
⬆ back to top
End of Time
Return the end of a unit of time for the given date.
moment().endOf("day");
import endOfDay from "date-fns/endOfDay";
endOfDay(new Date());
⬆ back to top
Display
Format
Return the formatted date string in the given format.
moment().format("dddd, MMMM Do YYYY, h:mm:ss A");
moment().format("ddd, hA");
import format from "date-fns/format";
format(new Date(), "eeee, MMMM do YYYY, h:mm:ss aa");
format(new Date(), "eee, ha");
⬆ back to top
Time from now
Return time from now.
moment([2018, 8, 9]).fromNow();
import formatDistance from "date-fns/formatDistance";
formatDistance(new Date(2018, 8, 9), new Date(), { addSuffix: true });
⬆ back to top
Time from x
Return time from x.
moment([2007, 0, 27]).to(moment([2007, 0, 29]));
import formatDistance from "date-fns/formatDistance";
formatDistance(new Date(2007, 0, 27), new Date(2007, 0, 29));
⬆ back to top
Difference
Get the unit of time between the given dates.
moment([2007, 0, 27]).diff(moment([2007, 0, 29]));
moment([2007, 0, 27]).diff(moment([2007, 0, 29]), "days");
import differenceInMilliseconds from "date-fns/differenceInMilliseconds";
differenceInMilliseconds(new Date(2007, 0, 27), new Date(2007, 0, 29));
import differenceInDays from "date-fns/differenceInDays";
differenceInDays(new Date(2007, 0, 27), new Date(2007, 0, 29));
⬆ back to top
Query
Is Before
Check if a date is before another date.
moment("2010-10-20").isBefore("2010-10-21");
import isBefore from "date-fns/isBefore";
isBefore(new Date(2010, 9, 20), new Date(2010, 9, 21));
⬆ back to top
Is Same
Check if a date is same another date.
moment("2010-10-20").isSame("2010-10-21");
moment("2010-10-20").isSame("2010-10-20");
import isSameDay from "date-fns/isSameDay";
isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 21));
isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 20));
⬆ back to top
Is After
Check if a date is after another date.
moment("2010-10-20").isAfter("2010-10-19");
import isAfter from "date-fns/isAfter";
isAfter(new Date(2010, 9, 20), new Date(2010, 9, 19));
⬆ back to top
Is Between
Check if a date is between two other dates.
moment("2010-10-20").isBetween("2010-10-19", "2010-10-25");
import isWithinInterval from "date-fns/isWithinInterval";
isWithinInterval(new Date(2010, 9, 20), {
start: new Date(2010, 9, 19),
end: new Date(2010, 9, 25)
});
⬆ back to top
Is Leap Year
Check if a year is a leap year.
moment([2000]).isLeapYear();
import isLeapYear from "date-fns/isLeapYear";
isLeapYear(new Date(2000, 0, 1));
⬆ back to top
Is a Date
Check if a variable is a native js Date object.
moment.isDate(new Date());
import isDate from "date-fns/isDate";
isDate(new Date());
⬆ back to top
License
MIT