What is date-fns?
The date-fns npm package provides a comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js. It offers a variety of functions to parse, validate, manipulate, and format dates.
What are date-fns's main functionalities?
Parsing Dates
Parse strings in ISO format to JavaScript Date objects.
const parseISO = require('date-fns/parseISO');
const result = parseISO('2023-04-12');
Formatting Dates
Format Date objects into strings with a given format.
const format = require('date-fns/format');
const result = format(new Date(2023, 3, 12), 'yyyy-MM-dd');
Comparing Dates
Compare two dates to determine if one comes before the other.
const isBefore = require('date-fns/isBefore');
const result = isBefore(new Date(2023, 3, 12), new Date(2023, 3, 13));
Manipulating Dates
Perform date calculations such as adding or subtracting time spans.
const addDays = require('date-fns/addDays');
const result = addDays(new Date(2023, 3, 12), 10);
Validating Dates
Check if a date is valid.
const isValid = require('date-fns/isValid');
const result = isValid(new Date('2023-04-12'));
Other packages similar to date-fns
moment
Moment.js is a legacy project, now in maintenance mode, which provides similar functionalities for parsing, validating, manipulating, and formatting dates. It's more object-oriented and mutable compared to the functional and immutable design of date-fns.
dayjs
Day.js is a lightweight date library that offers a similar API to Moment.js but with a smaller footprint. It is immutable and chainable, like date-fns, but with a different plugin system for extending functionality.
luxon
Luxon is a powerful, modern, and chainable library for working with dates and times. It offers a rich set of features for parsing, formatting, manipulating, and querying dates. It's built on the Intl API and provides time zone support out of the box, which is more comprehensive than date-fns's approach to time zones.
date-fns
Date helpers in function-per-file style.
Installation
npm install --save date-fns
Usage
var isLastDayOfMonth = require('date-fns/src/is_last_day_of_month');
var date = new Date(2014, 1, 28);
console.log(isLastDayOfMonth(date));
API
Code is fully documented, checkout source for reference.
Common helpers
format
- format date.isFuture
- is passed date future?isEqual
- are passed dates equal?isBefore
- is first date before second one?isAfter
- is first date after second one?compareAsc
- compares the two dates and returns -1, 0 or 1.compareDesc
- compares the two dates reverse chronologicaly and returns -1, 0 or 1.parse
- parse ISO-8601-formatted date.
Range helpers
Minutes helpers
Hours helpers
startOfHour
- returns start of an hour for passed date.endOfHour
- returns end of an hour for passed date.addHours
- add hours to passed date.subHours
- subtracts hours from passed date.
Day helpers
isWeekend
- is passed date weekend?isToday
- is passed date today?startOfDay
- returns start of a day for passed date.endOfDay
- returns end of a day for passed date.addDays
- add specified number of days to passed date.subDays
- subtract specified number of days from passed date.eachDay
- returns array of dates within specified range.isSameDay
- are parssed dates has the same day?
Week helpers
Month helpers
startOfMonth
- returns start of a month for passed date.endOfMonth
- returns end of a month for passed date.addMonths
- add specified number of months to passed date.subMonths
- subtract specified number of days from passed date.isSameMonth
- returns true if passed dates has same month (and year).isFirstDayOfMonth
- return true if passed date is first day of month.isLastDayOfMonth
- return true if passed date is last day of month.setMonth
- sets month index.
Year helpers
I18n
TODO