Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
date-fns
Advanced tools
Package description
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.
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'));
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.
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 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.
Changelog
[0.17.0] - 2015-09-29
Fixed a lot of bugs appearing when date is modifying into other time zone (e.g., when adding months and original date is in DST but new date is not).
Prevent instances of Date to lose milliseconds value when passed to.
parse
in IE10.
setISOWeek
now keeps time from original date.
Internal: reuse getDaysInMonth
inside of addMonths
.
differenceInDays
getTimeSinceMidnight
format
now has new format key aa
, which returns a.m.
/p.m.
as opposed to a
that returns am
/pm
.
Complete UMD package (for Bower and CDN).
Readme
Date helpers in function-per-file style.
npm install --save date-fns
var isLastDayOfMonth = require('date-fns/src/is_last_day_of_month')
var date = new Date(2014, 1, 28)
console.log(isLastDayOfMonth(date))
//=> true
Code is fully documented, checkout source for reference.
format
- format date.isFuture
- is passed date future?isPast
- is passed date past?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.max
- returns latest date.min
- returns earliest date.parse
- parse ISO-8601-formatted date.isWithinRange
- is passed date within range?getMilliseconds
- returns seconds.setMilliseconds
- sets seconds.addMilliseconds
- add milliseconds to passed date.subMilliseconds
- subtracts milliseconds from passed date.getTimeSinceMidnight
- returns time since midnight in milliseconds.getSeconds
- returns seconds.setSeconds
- sets seconds.startOfSecond
- returns start of a second for passed date.endOfSecond
- returns end of a second for passed date.addSeconds
- add seconds to passed date.subSeconds
- subtracts seconds from passed date.isSameSecond
- are passed dates have the same second?getMinutes
- returns minutes.setMinutes
- sets minutes.startOfMinute
- returns start of a minute for passed date.endOfMinute
- returns end of a minute for passed date.addMinutes
- add minutes to passed date.subMinutes
- subtracts minutes from passed date.isSameMinute
- are passed dates have the same minute?getHours
- returns hours.setHours
- sets hours.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.isSameHour
- are passed dates have the same hour?getDate
- returns day of month.setDate
- sets day of month.getDay
- returns day of week.setDay
- sets day of week.getDayOfYear
- returns day of year.setDayOfYear
- sets day of year.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.isSameDay
- are passed dates has the same day?isWeekend
- is passed date weekend?isToday
- is passed date today?eachDay
- returns array of dates within specified range.differenceInDays
- returns number of days between dates.getISOWeek
- returns ISO week index.setISOWeek
- sets ISO week index.startOfWeek
- returns start of a week for passed date.endOfWeek
- returns end of a week for passed date.lastDayOfWeek
- returns last day of week for passed date.addWeeks
- add specified number of weeks to passed date.subWeeks
- subtract specified number of weeks from passed date.isSameWeek
- returns true if passed dates belongs to the same week.getMonth
- returns month index.setMonth
- sets month index.startOfMonth
- returns start of a month for passed date.endOfMonth
- returns end of a month for passed date.lastDayOfMonth
- returns last day of month for passed date.addMonths
- add specified number of months to passed date.subMonths
- subtract specified number of months from passed date.isSameMonth
- returns true if passed dates have 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.getDaysInMonth
- returns number of days in month.getQuarter
- returns year quarter.setQuarter
- sets year quarter.startOfQuarter
- returns start of year quarter for passed date.endOfQuarter
- returns end of year quarter for passed date.lastDayOfQuarter
- returns last day of year quarter for passed date.addQuarters
- add specified number of year quarters to passed date.subQuarters
- subtract specified number of year quarters from passed date.isSameQuarter
- are passed dates have the same year quarter?getYear
- returns full year.setYear
- sets full year.getISOYear
- returns ISO week-numbering year.setISOYear
- sets ISO week-numbering year.startOfYear
- returns start of year for passed date.startOfISOYear
- returns start of ISO week-numbering year for passed date.endOfYear
- returns end of year for passed date.lastDayOfYear
- returns last day of year for passed date.addYears
- add specified number of years to passed date.subYears
- subtract specified number of years from passed date.isSameYear
- are passed dates have the same year?isLeapYear
- returns true is passed date is in the leap year.getISOWeeksInYear
- returns number of weeks in ISO week-numbering year.TODO
FAQs
Modern JavaScript date utility library
The npm package date-fns receives a total of 16,131,754 weekly downloads. As such, date-fns popularity was classified as popular.
We found that date-fns demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.