But you should use date-fns.
Not only it’s a great library, battle tested and with a strong team, but also dates in javascript are a pain to work with, no point in dealing with them at a low level when there are such well crafted libraries around. The goal of this project is NOT to provide drop in replacements, but to show how achieve similar functionalities in plain Javascript, to understand how things work behind the hood.
Being a learning platform, some implementations have been simplified to make them more digestible, and they might miss edge cases covered in the original version.
On top of that there are a lot of methods yet to be ported including what’s probably the most needed: format; please help contributing on github.
// https://date-fns.org/v3.5.0/docs/closestIndexTo
import { closestIndexTo } from 'date-fns'
// Which date is closer to 6 September 2015?
const dateToCompare = new Date(2015, 8, 6)
const datesArray = [
new Date(2015, 0, 1),
new Date(2016, 0, 1),
new Date(2017, 0, 1),
]
closestIndexTo(dateToCompare, datesArray)
// => 1
const closestIndexTo = (dateToCompare, datesArray) => {
const distances = datesArray.map(date => Math.abs(date - dateToCompare))
return distances.indexOf(Math.min(...distances))
}
// Which date is closer to 6 September 2015?
const dateToCompare = new Date(2015, 8, 6)
const datesArray = [
new Date(2015, 0, 1),
new Date(2016, 0, 1),
new Date(2017, 0, 1),
]
closestIndexTo(dateToCompare, datesArray)
// => 1
// https://date-fns.org/v3.5.0/docs/closestTo
import { closestTo } from 'date-fns'
// Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?
const dateToCompare = new Date(2015, 8, 6)
closestTo(dateToCompare, [
new Date(2000, 0, 1),
new Date(2030, 0, 1),
])
// => Tue Jan 01 2030 00:00:00
const closestTo = (dateToCompare, datesArray) => {
const distances = datesArray.map(date => Math.abs(date - dateToCompare))
return datesArray[distances.indexOf(Math.min(...distances))]
}
// Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?
const dateToCompare = new Date(2015, 8, 6)
closestTo(dateToCompare, [
new Date(2000, 0, 1),
new Date(2030, 0, 1),
])
// => Tue Jan 01 2030 00:00:00
// https://date-fns.org/v3.5.0/docs/compareAsc
import { compareAsc } from 'date-fns'
// Compare 11 February 1987 and 11 February 1987:
compareAsc(new Date(1987, 1, 11), new Date(1987, 1, 11))
// => 0
// Compare 11 February 1987 and 10 July 1989:
compareAsc(new Date(1987, 1, 11), new Date(1989, 6, 10))
// => -1
// Sort the array of dates:
[
new Date(1995, 6, 2),
new Date(1987, 1, 11),
new Date(1989, 6, 10),
].sort(compareAsc)
// => [
// Wed Feb 11 1987 00:00:00,
// Mon Jul 10 1989 00:00:00,
// Sun Jul 02 1995 00:00:00
// ]
const compareAsc = (dateA, dateB) => {
if (dateA.getTime() === dateB.getTime()) return 0
return dateA > dateB ? 1 : -1
}
// Compare 11 February 1987 and 11 February 1987:
compareAsc(new Date(1987, 1, 11), new Date(1987, 1, 11))
// => 0
// Compare 11 February 1987 and 10 July 1989:
compareAsc(new Date(1987, 1, 11), new Date(1989, 6, 10))
// => -1
// Sort the array of dates:
[
new Date(1995, 6, 2),
new Date(1987, 1, 11),
new Date(1989, 6, 10),
].sort(compareAsc)
// => [
// Wed Feb 11 1987 00:00:00,
// Mon Jul 10 1989 00:00:00,
// Sun Jul 02 1995 00:00:00
// ]
// https://date-fns.org/v3.5.0/docs/compareDesc
import { compareDesc } from 'date-fns'
// Compare 11 February 1987 and 11 February 1987:
compareDesc(new Date(1987, 1, 11), new Date(1987, 1, 11))
// => 0
// Compare 11 February 1987 and 10 July 1989:
compareDesc(new Date(1987, 1, 11), new Date(1989, 6, 10))
// => -1
// Sort the array of dates in reverse chronological order:
[
new Date(1995, 6, 2),
new Date(1987, 1, 11),
new Date(1989, 6, 10),
].sort(compareDesc)
// => [
// Sun Jul 02 1995 00:00:00,
// Mon Jul 10 1989 00:00:00,
// Wed Feb 11 1987 00:00:00
// ]
const compareDesc = (dateA, dateB) => {
if (dateA.getTime() === dateB.getTime()) return 0
return dateA > dateB ? -1 : 1
}
// Compare 11 February 1987 and 11 February 1987:
compareDesc(new Date(1987, 1, 11), new Date(1987, 1, 11))
// => 0
// Compare 11 February 1987 and 10 July 1989:
compareDesc(new Date(1987, 1, 11), new Date(1989, 6, 10))
// => -1
// Sort the array of dates in reverse chronological order:
[
new Date(1995, 6, 2),
new Date(1987, 1, 11),
new Date(1989, 6, 10),
].sort(compareDesc)
// => [
// Sun Jul 02 1995 00:00:00,
// Mon Jul 10 1989 00:00:00,
// Wed Feb 11 1987 00:00:00
// ]
// https://date-fns.org/v3.5.0/docs/isAfter
import { isAfter } from 'date-fns'
isAfter(new Date(1987, 6, 10), new Date(1989, 1, 11))
// => true
isAfter(new Date(1989, 6, 10), new Date(1989, 6, 10))
// => false
isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11))
// => true
// https://date-fns.org/v3.5.0/docs/isBefore
import { isBefore } from 'date-fns'
isBefore(new Date(1987, 6, 10), new Date(1989, 1, 11))
// => true
isBefore(new Date(1989, 6, 10), new Date(1989, 6, 10))
// => false
isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11))
// => false
// https://date-fns.org/v3.5.0/docs/isDate
import { isDate } from 'date-fns'
// For a valid date:
isDate(new Date())
// => true
// For an invalid date:
isDate(new Date(NaN))
// => true
// For some value:
isDate('2014-02-31')
// => false
// For an object:
isDate({})
// => false
// https://date-fns.org/v3.5.0/docs/isEqual
import { isEqual } from 'date-fns'
// Are 2 July 2014 06:30:45.000 and 2 July 2014 06:30:45.500 equal?
isEqual(
new Date(2014, 6, 2, 6, 30, 45, 0),
new Date(2014, 6, 2, 6, 30, 45, 500)
)
// => false
isEqual(
new Date(2014, 6, 2, 6, 30, 45, 500),
new Date(2014, 6, 2, 6, 30, 45, 500)
)
// => true
const isEqual = (dateA, dateB) => dateA.getTime() === dateB.getTime()
// Are 2 July 2014 06:30:45.000 and 2 July 2014 06:30:45.500 equal?
isEqual(
new Date(2014, 6, 2, 6, 30, 45, 0),
new Date(2014, 6, 2, 6, 30, 45, 500)
)
// => false
isEqual(
new Date(2014, 6, 2, 6, 30, 45, 500),
new Date(2014, 6, 2, 6, 30, 45, 500)
)
// => true
// https://date-fns.org/v3.5.0/docs/isExists
import { isExists } from 'date-fns'
// For the valid date:
isExists(2018, 0, 31)
// => true
// For the invalid date:
isExists(2018, 1, 31)
// => false
// https://date-fns.org/v3.5.0/docs/isFuture
import { isFuture } from 'date-fns'
// If today is 25 September 2014, Is 31 December 2014 in the future?
isFuture(new Date(2014, 11, 31))
// => true
// If today is 25 September 2014, Is 31 December 1980 in the future?
isFuture(new Date(1980, 11, 31))
// => false
// https://date-fns.org/v3.5.0/docs/isPast
import { isPast } from 'date-fns'
// If today is 25 September 2014, Is 2 July 2014 in the past?
isPast(new Date(2014, 6, 2))
// => true
// If today is 25 September 2014, Is 2 July 2015 in the past?
isPast(new Date(2015, 6, 2))
// => false
// https://date-fns.org/v3.5.0/docs/isValid
import { isValid } from 'date-fns'
// For the valid date:
isValid(new Date(2014, 1, 31))
// => true
// For the value, convertable into a date:
isValid(1393804800000)
// => true
// For the invalid date:
isValid(new Date(''))
// => false
/* eslint-disable no-restricted-globals */
const isValid = date =>
!isNaN((date instanceof Date ? date : new Date(date)).getTime())
// For the valid date:
isValid(new Date(2014, 1, 31))
// => true
// For the value, convertable into a date:
isValid(1393804800000)
// => true
// For the invalid date:
isValid(new Date(''))
// => false
// https://date-fns.org/v3.5.0/docs/max
import { max } from 'date-fns'
// Which of these dates is the latest?
max([
new Date(1989, 6, 10),
new Date(1987, 1, 11),
new Date(1995, 6, 2),
new Date(1990, 0, 1),
])
// => Sun Jul 02 1995 00:00:00
// https://date-fns.org/v3.5.0/docs/min
import { min } from 'date-fns'
// Which of these dates is the earliest?
min([
new Date(1989, 6, 10),
new Date(1987, 1, 11),
new Date(1995, 6, 2),
new Date(1990, 0, 1),
])
// => Wed Feb 11 1987 00:00:00
Is the given time interval overlapping with another time interval? Adjacent intervals do not count as overlapping unless inclusive is set to true.
// https://date-fns.org/v3.5.0/docs/areIntervalsOverlapping
import { areIntervalsOverlapping } from 'date-fns'
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
)
//=> true
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
)
//=> false
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) }
)
//=> false
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
{ inclusive: true }
)
//=> true
const areIntervalsOverlapping = (
{ start: rxStart, end: rxEnd },
{ start: ryStart, end: ryEnd },
{ inclusive } = {}
) =>
!!(
(ryStart > rxStart && ryStart < rxEnd) ||
(ryEnd > rxStart && ryStart < rxEnd) ||
(inclusive && rxEnd.getTime() === ryStart.getTime()) ||
(inclusive && ryEnd.getTime() === rxStart.getTime())
)
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
)
//=> true
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
)
//=> false
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 20), end: new Date(2014, 0, 30) }
)
//=> false
areIntervalsOverlapping(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
{ inclusive: true }
)
//=> true
Resources: Date#getTime
// https://date-fns.org/v3.5.0/docs/eachDayOfInterval
import { eachDayOfInterval } from 'date-fns'
eachDayOfInterval({
start: new Date(2014, 9, 6),
end: new Date(2014, 9, 10),
})
//=> [
// Mon Oct 06 2014 00:00:00,
// Tue Oct 07 2014 00:00:00,
// Wed Oct 08 2014 00:00:00,
// Thu Oct 09 2014 00:00:00,
// Fri Oct 10 2014 00:00:00
// ]
eachDayOfInterval(
{
start: new Date(2014, 9, 6),
end: new Date(2014, 9, 10),
},
{ step: 3 }
)
//=> [
// Wed Oct 06 2014 00:00:00,
// Mon Oct 09 2014 00:00:00,
// ]
const eachDayOfInterval = ({ start, end }, { step } = { step: 1 }) => {
const retArr = []
start.setHours(0, 0, 0, 0)
end.setHours(0, 0, 0, 0)
do {
retArr.push(new Date(start.getTime()))
start.setDate(start.getDate() + step)
} while (start.getTime() <= end.getTime())
return retArr
}
eachDayOfInterval({
start: new Date(2014, 9, 6),
end: new Date(2014, 9, 10),
})
//=> [
// Mon Oct 06 2014 00:00:00,
// Tue Oct 07 2014 00:00:00,
// Wed Oct 08 2014 00:00:00,
// Thu Oct 09 2014 00:00:00,
// Fri Oct 10 2014 00:00:00
// ]
eachDayOfInterval(
{
start: new Date(2014, 9, 6),
end: new Date(2014, 9, 10),
},
{ step: 3 }
)
//=> [
// Wed Oct 06 2014 00:00:00,
// Mon Oct 08 2014 00:00:00,
// ]
// https://date-fns.org/v3.5.0/docs/isWithinInterval
import { isWithinInterval } from 'date-fns'
isWithinInterval(new Date(2014, 0, 3), {
start: new Date(2014, 0, 1),
end: new Date(2014, 0, 7),
})
//=> true
isWithinInterval(new Date(2014, 0, 10), {
start: new Date(2014, 0, 1),
end: new Date(2014, 0, 7),
})
//=> false
const date = new Date()
isWithinInterval(date, {
start: date,
end: new Date(2500, 0, 1),
})
// => true
isWithinInterval(date, {
start: new Date(2020, 0, 1),
end: date,
})
// => true
const isWithinInterval = (date, { start, end }) =>
date.getTime() >= start.getTime() && date.getTime() <= end.getTime()
isWithinInterval(new Date(2014, 0, 3), {
start: new Date(2014, 0, 1),
end: new Date(2014, 0, 7),
})
//=> true
isWithinInterval(new Date(2014, 0, 10), {
start: new Date(2014, 0, 1),
end: new Date(2014, 0, 7),
})
//=> false
// new Date is mocked as
// new Date(2014, 8, 25, 18, 30, 15, 500)
const date = new Date()
isWithinInterval(date, {
start: date,
end: new Date(2020, 0, 1),
})
// => true
isWithinInterval(date, {
start: new Date(2000, 0, 1),
end: date,
})
// => true
// https://date-fns.org/v3.5.0/docs/fromUnixTime
import { fromUnixTime } from 'date-fns'
// Create the date 29 February 2012 11:45:05:
fromUnixTime(1330515905)
// => Wed Feb 29 2012 11:45:05
// https://date-fns.org/v3.5.0/docs/getTime
import { getTime } from 'date-fns'
// Get the timestamp of 29 February 2012 11:45:05.123:
getTime(new Date(Date.UTC(2012, 1, 29, 11, 45, 5, 123)))
// => 1330515905123
// https://date-fns.org/v3.5.0/docs/getUnixTime
import { getUnixTime } from 'date-fns'
// Get the timestamp of 29 February 2012 11:45:05 CET:
getUnixTime(new Date(Date.UTC(2012, 1, 29, 11, 45, 5)))
// => 1330515905
// https://date-fns.org/v3.5.0/docs/addMilliseconds
import { addMilliseconds } from 'date-fns'
// Add 750 milliseconds to 10 July 2014 12:45:30.000:
addMilliseconds(new Date(2014, 6, 10, 12, 45, 30, 0), 750)
// => Thu Jul 10 2014 12:45:30.750
// https://date-fns.org/v3.5.0/docs/differenceInMilliseconds
import { differenceInMilliseconds } from 'date-fns'
// How many milliseconds are between
// 2 July 2014 12:30:20.600 and 2 July 2014 12:30:21.700?
differenceInMilliseconds(
new Date(2014, 6, 2, 12, 30, 21, 700),
new Date(2014, 6, 2, 12, 30, 20, 600)
)
// => 1100
// https://date-fns.org/v3.5.0/docs/getMilliseconds
import { getMilliseconds } from 'date-fns'
// Get the milliseconds of 29 February 2012 11:45:05.123:
getMilliseconds(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 123
// https://date-fns.org/v3.5.0/docs/setMilliseconds
import { setMilliseconds } from 'date-fns'
// Set 300 milliseconds to 1 September 2014 11:30:40.500:
setMilliseconds(new Date(2014, 8, 1, 11, 30, 40, 500), 300)
// => Mon Sep 01 2014 11:30:40.300
// https://date-fns.org/v3.5.0/docs/subMilliseconds
import { subMilliseconds } from 'date-fns'
// Subtract 750 milliseconds from 10 July 2014 12:45:30.000:
subMilliseconds(new Date(2014, 6, 10, 12, 45, 30, 0), 750)
// => Thu Jul 10 2014 12:45:29.250
// https://date-fns.org/v3.5.0/docs/addSeconds
import { addSeconds } from 'date-fns'
// Add 30 seconds to 10 July 2014 12:45:00:
addSeconds(new Date(2014, 6, 10, 12, 45, 0), 30)
// => Thu Jul 10 2014 12:45:30
// https://date-fns.org/v3.5.0/docs/differenceInSeconds
import { differenceInSeconds } from 'date-fns'
// How many seconds are between
// 2 July 2014 12:30:07.999 and 2 July 2014 12:30:20.000?
differenceInSeconds(
new Date(2014, 6, 2, 12, 30, 20, 0),
new Date(2014, 6, 2, 12, 30, 7, 999)
)
// => 12
// https://date-fns.org/v3.5.0/docs/endOfSecond
import { endOfSecond } from 'date-fns'
// The end of a second for 1 December 2014 22:15:45.400:
endOfSecond(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:45.999
// https://date-fns.org/v3.5.0/docs/getSeconds
import { getSeconds } from 'date-fns'
// Get the seconds of 29 February 2012 11:45:05.123:
getSeconds(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 5
// https://date-fns.org/v3.5.0/docs/isSameSecond
import { isSameSecond } from 'date-fns'
// Are 4 September 2014 06:30:15.000 and 4 September 2014 06:30.15.500
// in the same second?
isSameSecond(
new Date(2014, 8, 4, 6, 30, 15),
new Date(2014, 8, 4, 6, 30, 15, 500)
)
// => true
const isSameSecond = (dateA, dateB) =>
Math.abs(dateA - dateB) < 1000 && dateA.getSeconds() === dateB.getSeconds()
// Are 4 September 2014 06:30:15.000 and 4 September 2014 06:30.15.500
// in the same second?
isSameSecond(
new Date(2014, 8, 4, 6, 30, 15, 0),
new Date(2014, 8, 4, 6, 30, 15, 500)
)
// => true
// https://date-fns.org/v3.5.0/docs/isThisSecond
import { isThisSecond } from 'date-fns'
// If now is 25 September 2014 18:30:15.500,
// is 25 September 2014 18:30:15.000 in this second?
isThisSecond(new Date(2014, 8, 25, 18, 30, 15))
// => true
// https://date-fns.org/v3.5.0/docs/setSeconds
import { setSeconds } from 'date-fns'
// Set 45 seconds to 1 September 2014 11:30:40:
setSeconds(new Date(2014, 8, 1, 11, 30, 40), 45)
// => Mon Sep 01 2014 11:30:45
// https://date-fns.org/v3.5.0/docs/startOfSecond
import { startOfSecond } from 'date-fns'
// The start of a second for 1 December 2014 22:15:45.400:
startOfSecond(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:45.000
// https://date-fns.org/v3.5.0/docs/subSeconds
import { subSeconds } from 'date-fns'
// Subtract 30 seconds from 10 July 2014 12:45:00:
subSeconds(new Date(2014, 6, 10, 12, 45, 0), 30)
// => Thu Jul 10 2014 12:44:30
// https://date-fns.org/v3.5.0/docs/addMinutes
import { addMinutes } from 'date-fns'
// Add 30 minutes to 10 July 2014 12:00:00:
addMinutes(new Date(2014, 6, 10, 12, 0), 30)
// => Thu Jul 10 2014 12:30:00
// https://date-fns.org/v3.5.0/docs/differenceInMinutes
import { differenceInMinutes } from 'date-fns'
// How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
differenceInMinutes(
new Date(2014, 6, 2, 12, 20, 0),
new Date(2014, 6, 2, 12, 7, 59)
)
// => 12
// How many minutes are from 10:01:59 to 10:00:00
differenceInMinutes(
new Date(2000, 0, 1, 10, 0, 0),
new Date(2000, 0, 1, 10, 1, 59)
)
// => -1
const differenceInMinutes = (dateA, dateB) =>
Math.trunc((dateA - dateB) / (1000 * 60))
// How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
differenceInMinutes(
new Date(2014, 6, 2, 12, 20, 0),
new Date(2014, 6, 2, 12, 7, 59)
)
// => 12
// How many minutes are from 10:01:59 to 10:00:00
differenceInMinutes(
new Date(2000, 0, 1, 10, 0, 0),
new Date(2000, 0, 1, 10, 1, 59)
)
// => -1
// https://date-fns.org/v3.5.0/docs/endOfMinute
import { endOfMinute } from 'date-fns'
// The end of a minute for 1 December 2014 22:15:45.400:
endOfMinute(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:59.999
// https://date-fns.org/v3.5.0/docs/getMinutes
import { getMinutes } from 'date-fns'
// Get the minutes of 29 February 2012 11:45:05:
getMinutes(new Date(2012, 1, 29, 11, 45, 5))
// => 45
// https://date-fns.org/v3.5.0/docs/isSameMinute
import { isSameMinute } from 'date-fns'
// Are 4 September 2014 06:30:00 and 4 September 2014 06:30:15
// in the same minute?
isSameMinute(
new Date(2014, 8, 4, 6, 30),
new Date(2014, 8, 4, 6, 30, 15)
)
// => true
const isSameMinute = (dateA, dateB) =>
Math.abs(dateA - dateB) < 1000 * 60 &&
dateA.getMinutes() === dateB.getMinutes()
// Are 4 September 2014 06:30:00 and 4 September 2014 06:30:15
// in the same minute?
isSameMinute(
new Date(2014, 8, 4, 6, 30),
new Date(2014, 8, 4, 6, 30, 15)
)
// => true
// https://date-fns.org/v3.5.0/docs/isThisMinute
import { isThisMinute } from 'date-fns'
// If now is 25 September 2014 18:30:15.500,
// is 25 September 2014 18:30:00 in this minute?
isThisMinute(new Date(2014, 8, 25, 18, 30))
// => true
// https://date-fns.org/v3.5.0/docs/roundToNearestMinutes
import { roundToNearestMinutes } from 'date-fns'
// Round 10 July 2014 12:12:34 to nearest minute:
roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
// => Thu Jul 10 2014 12:13:00
// https://date-fns.org/v3.5.0/docs/setMinutes
import { setMinutes } from 'date-fns'
// Set 45 minutes to 1 September 2014 11:30:40:
setMinutes(new Date(2014, 8, 1, 11, 30, 40), 45)
// => Mon Sep 01 2014 11:45:40
// https://date-fns.org/v3.5.0/docs/startOfMinute
import { startOfMinute } from 'date-fns'
// The start of a minute for 1 December 2014 22:15:45.400:
startOfMinute(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:00
// https://date-fns.org/v3.5.0/docs/subMinutes
import { subMinutes } from 'date-fns'
// Subtract 30 minutes from 10 July 2014 12:00:00:
subMinutes(new Date(2014, 6, 10, 12, 0), 30)
// => Thu Jul 10 2014 11:30:00
// https://date-fns.org/v3.5.0/docs/addHours
import { addHours } from 'date-fns'
// Add 2 hours to 10 July 2014 23:00:00:
addHours(new Date(2014, 6, 10, 23, 0), 2)
// => Fri Jul 11 2014 01:00:00
// https://date-fns.org/v3.5.0/docs/differenceInHours
import { differenceInHours } from 'date-fns'
// How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
differenceInHours(
new Date(2014, 6, 2, 19, 0),
new Date(2014, 6, 2, 6, 50)
)
// => 12
// https://date-fns.org/v3.5.0/docs/endOfHour
import { endOfHour } from 'date-fns'
// The end of an hour for 2 September 2014 11:55:00:
endOfHour(new Date(2014, 8, 2, 11, 55))
// => Tue Sep 02 2014 11:59:59.999
// https://date-fns.org/v3.5.0/docs/getHours
import { getHours } from 'date-fns'
// Get the hours of 29 February 2012 11:45:00:
getHours(new Date(2012, 1, 29, 11, 45))
// => 11
// https://date-fns.org/v3.5.0/docs/isSameHour
import { isSameHour } from 'date-fns'
// Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour?
isSameHour(
new Date(2014, 8, 4, 6, 0),
new Date(2014, 8, 4, 6, 30)
)
// => true
const HOUR_IN_MS = 1000 * 60 * 60
const isSameHour = (dateA, dateB) =>
Math.abs(dateA - dateB) < HOUR_IN_MS && dateA.getHours() === dateB.getHours()
// Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour?
isSameHour(
new Date(2014, 8, 4, 6, 0),
new Date(2014, 8, 4, 6, 30)
)
// => true
// https://date-fns.org/v3.5.0/docs/isThisHour
import { isThisHour } from 'date-fns'
// If now is 25 September 2014 18:30:15.500,
// is 25 September 2014 18:00:00 in this hour?
isThisHour(new Date(2014, 8, 25, 18))
// => true
const HOUR_IN_MS = 1000 * 60 * 60
const isThisHour = (dateA, dateB = new Date()) =>
Math.abs(dateA - dateB) < HOUR_IN_MS && dateA.getHours() === dateB.getHours()
// If now is 25 September 2014 18:30:15.500,
// is 25 September 2014 18:00:00 in this hour?
isThisHour(new Date(2014, 8, 25, 18))
// => true
// https://date-fns.org/v3.5.0/docs/setHours
import { setHours } from 'date-fns'
// Set 4 hours to 1 September 2014 11:30:00:
setHours(new Date(2014, 8, 1, 11, 30), 4)
// => Mon Sep 01 2014 04:30:00
// https://date-fns.org/v3.5.0/docs/startOfHour
import { startOfHour } from 'date-fns'
// The start of an hour for 2 September 2014 11:55:00:
startOfHour(new Date(2014, 8, 2, 11, 55))
// => Tue Sep 02 2014 11:00:00
// https://date-fns.org/v3.5.0/docs/subHours
import { subHours } from 'date-fns'
// Subtract 2 hours from 11 July 2014 01:00:00:
subHours(new Date(2014, 6, 11, 1, 0), 2)
// => Thu Jul 10 2014 23:00:00
// https://date-fns.org/v3.5.0/docs/addDays
import { addDays } from 'date-fns'
// Add 10 days to 1 September 2014:
addDays(new Date(2014, 8, 1), 10)
// => Thu Sep 11 2014 00:00:00
// https://date-fns.org/v3.5.0/docs/differenceInDays
import { differenceInDays } from 'date-fns'
// How many full days are between
// 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
differenceInDays(
new Date(2012, 6, 2, 0, 0),
new Date(2011, 6, 2, 23, 0)
)
// => 365
// How many full days are between
// 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
differenceInDays(
new Date(2011, 6, 3, 0, 1),
new Date(2011, 6, 2, 23, 59)
)
// => 0
// How many full days are between
// 1 March 2020 0:00 and 1 June 2020 0:00 ?
differenceInDays(new Date(2020, 5, 1), new Date(2020, 2, 1))
// => 92
const differenceInDays = (dateA, dateB) =>
Math.round((dateA - dateB) / (1000 * 60 * 60 * 24))
// How many full days are between
// 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
differenceInDays(
new Date(2012, 6, 2, 0, 0),
new Date(2011, 6, 2, 23, 0)
)
// => 365
// How many full days are between
// 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
differenceInDays(
new Date(2011, 6, 3, 0, 1),
new Date(2011, 6, 2, 23, 59)
)
// => 0
// How many full days are between
// 1 March 2020 0:00 and 1 June 2020 0:00 ?
differenceInDays(new Date(2020, 5, 1), new Date(2020, 2, 1))
// => 92
// https://date-fns.org/v3.5.0/docs/getDate
import { getDate } from 'date-fns'
// Which day of the month is 29 February 2012?
getDate(new Date(2012, 1, 29))
// => 29
// https://date-fns.org/v3.5.0/docs/getDayOfYear
import { getDayOfYear } from 'date-fns'
// Which day of the year is 2 July 2014?
getDayOfYear(new Date(2014, 6, 2))
// => 183
// https://date-fns.org/v3.5.0/docs/isSameDay
import { isSameDay } from 'date-fns'
// Are 4 September 06:00:00 and 4 September 18:00:00 in the same day?
isSameDay(
new Date(2014, 8, 4, 6, 0),
new Date(2014, 8, 4, 18, 0)
)
// => true
// https://date-fns.org/v3.5.0/docs/isToday
import { isToday } from 'date-fns'
// If today is 25 September 2014, is 25 September 14:00 today?
isToday(new Date(2014, 8, 25, 14, 0))
// => true
// https://date-fns.org/v3.5.0/docs/isTomorrow
import { isTomorrow } from 'date-fns'
// If today is 25 September 2014, is 24 September tomorrow?
isTomorrow(new Date(2014, 8, 26, 14, 0))
// => true
// https://date-fns.org/v3.5.0/docs/isYesterday
import { isYesterday } from 'date-fns'
// If today is 25 September 2014, is 24 September yesterday?
isYesterday(new Date(2014, 8, 24, 14, 0))
// => true
// https://date-fns.org/v3.5.0/docs/setDate
import { setDate } from 'date-fns'
// Set the 30th day of the month to 1 September 2014:
setDate(new Date(2014, 8, 1), 30)
// => Tue Sep 30 2014 00:00:00
// https://date-fns.org/v3.5.0/docs/subDays
import { subDays } from 'date-fns'
// Subtract 10 days from 1 September 2014:
subDays(new Date(2014, 8, 1), 10)
// => Fri Aug 22 2014 00:00:00
// https://date-fns.org/v3.5.0/docs/getDay
import { getDay } from 'date-fns'
// Which day of the week is 29 February 2012?
getDay(new Date(2012, 1, 29))
// => 3
// Which day of the week is 26 February 2012?
getDay(new Date(2012, 1, 26))
// => 0
// https://date-fns.org/v3.5.0/docs/getISODay
import { getISODay } from 'date-fns'
// Which day of the week is 29 February 2012?
getISODay(new Date(2012, 1, 29))
// => 3
// Which day of the week is 26 February 2012?
getISODay(new Date(2012, 1, 26))
// => 7
// https://date-fns.org/v3.5.0/docs/isFriday
import { isFriday } from 'date-fns'
// Is 26 September 2014 Friday?
isFriday(new Date(2014, 8, 26))
// => true
Is the given date Monday? (you could also check Mondays)
// https://date-fns.org/v3.5.0/docs/isMonday
import { isMonday } from 'date-fns'
// Is 22 September 2014 Monday?
isMonday(new Date(2014, 8, 22))
// => true
// https://date-fns.org/v3.5.0/docs/isSaturday
import { isSaturday } from 'date-fns'
// Is 27 September 2014 Saturday?
isSaturday(new Date(2014, 8, 27))
// => true
// https://date-fns.org/v3.5.0/docs/isSunday
import { isSunday } from 'date-fns'
// Is 21 September 2014 Sunday?
isSunday(new Date(2014, 8, 21))
// => true
// https://date-fns.org/v3.5.0/docs/isThursday
import { isThursday } from 'date-fns'
// Is 25 September 2014 Thursday?
isThursday(new Date(2014, 8, 25))
// => true
// https://date-fns.org/v3.5.0/docs/isTuesday
import { isTuesday } from 'date-fns'
// Is 23 September 2014 Tuesday?
isTuesday(new Date(2014, 8, 23))
// => true
// https://date-fns.org/v3.5.0/docs/isWednesday
import { isWednesday } from 'date-fns'
// Is 24 September 2014 Wednesday?
isWednesday(new Date(2014, 8, 24))
// => true
// https://date-fns.org/v3.5.0/docs/isWeekend
import { isWeekend } from 'date-fns'
// Does 5 October 2014 fall on a weekend?
isWeekend(new Date(2014, 9, 5))
// => true
// https://date-fns.org/v3.5.0/docs/addWeeks
import { addWeeks } from 'date-fns'
// Add 4 weeks to 1 September 2014:
addWeeks(new Date(2014, 8, 1), 4)
// => Mon Sep 29 2014 00:00:00
// https://date-fns.org/v3.5.0/docs/subWeeks
import { subWeeks } from 'date-fns'
// Subtract 4 weeks from 1 September 2014:
subWeeks(new Date(2014, 8, 1), 4)
// => Mon Aug 04 2014 00:00:00
// https://date-fns.org/v3.5.0/docs/addMonths
import { addMonths } from 'date-fns'
// Add 5 months to 1 September 2014:
addMonths(new Date(2014, 8, 1), 5)
// => Sun Feb 01 2015 00:00:00
// https://date-fns.org/v3.5.0/docs/getMonth
import { getMonth } from 'date-fns'
// Which month is 29 February 2012?
getMonth(new Date(2012, 1, 29))
// => 1
// https://date-fns.org/v3.5.0/docs/isFirstDayOfMonth
import { isFirstDayOfMonth } from 'date-fns'
// Is 1 September 2014 the first day of a month?
isFirstDayOfMonth(new Date(2014, 8, 1))
// => true
// https://date-fns.org/v3.5.0/docs/isLastDayOfMonth
import { isLastDayOfMonth } from 'date-fns'
// Is 28 February 2014 the last day of a month?
isLastDayOfMonth(new Date(2014, 1, 28))
// => true
// https://date-fns.org/v3.5.0/docs/isSameMonth
import { isSameMonth } from 'date-fns'
// Are 2 September 2014 and 25 September 2014 in the same month?
isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25))
// => true
// https://date-fns.org/v3.5.0/docs/isThisMonth
import { isThisMonth } from 'date-fns'
// If today is 25 September 2014, is 15 September 2014 in this month?
isThisMonth(new Date(2014, 8, 15))
// => true
// https://date-fns.org/v2.29.3/docs/lastDayOfMonth
import { lastDayOfMonth } from 'date-fns'
// Get the last day of a month.
lastDayOfMonth(new Date(2014, 1, 25)).toDateString()
// => Fri Feb 28 2014
function lastDayOfMonth(date) {
const dateClone = new Date(date.getTime())
const month = dateClone.getMonth()
dateClone.setDate(1)
dateClone.setMonth(month + 1)
dateClone.setDate(0)
return dateClone
}
// Get the last day of a month.
lastDayOfMonth(new Date(2014, 1, 25)).toDateString()
// => Fri Feb 28 2014
// https://date-fns.org/v3.5.0/docs/setMonth
import { setMonth } from 'date-fns'
// Set February to 1 September 2014:
setMonth(new Date(2014, 8, 1), 1)
// => Sat Feb 01 2014 00:00:00
// https://date-fns.org/v3.5.0/docs/subMonths
import { subMonths } from 'date-fns'
// Subtract 5 months from 1 February 2015:
subMonths(new Date(2015, 1, 1), 5)
// => Mon Sep 01 2014 00:00:00
// https://date-fns.org/v3.5.0/docs/addYears
import { addYears } from 'date-fns'
// Add 5 years to 1 September 2014:
addYears(new Date(2014, 8, 1), 5)
// => Sun Sep 01 2019 00:00:00
// https://date-fns.org/v3.5.0/docs/differenceInCalendarYears
import { differenceInCalendarYears } from 'date-fns'
// How many calendar years are between 31 December 2013 and 11 February 2015?
differenceInCalendarYears(
new Date(2015, 1, 11),
new Date(2013, 11, 31)
)
// => 2
// https://date-fns.org/v3.5.0/docs/differenceInYears
import { differenceInYears } from 'date-fns'
// How many full years are between 31 December 2013 and 11 February 2015?
differenceInYears(
new Date(2015, 1, 11),
new Date(2013, 11, 31)
)
// => 1
// https://date-fns.org/v3.5.0/docs/getYear
import { getYear } from 'date-fns'
// Which year is 2 July 2014?
getYear(new Date(2014, 6, 2))
// => 2014
// https://date-fns.org/v3.5.0/docs/isLeapYear
import { isLeapYear } from 'date-fns'
// Is 1 September 2012 in the leap year?
isLeapYear(new Date(2012, 8, 1))
// => true
// Is 1 September 2013 in the leap year?
isLeapYear(new Date(2013, 8, 1))
// => false
const isLeapYear = date => {
const year = date.getFullYear()
return year % 100 === 0 ? year % 400 === 0 : year % 4 === 0
}
// Is 1 September 2012 in the leap year?
isLeapYear(new Date(2012, 8, 1))
// => true
// Is 1 September 2013 in the leap year?
isLeapYear(new Date(2013, 8, 1))
// => false
// https://date-fns.org/v3.5.0/docs/isSameYear
import { isSameYear } from 'date-fns'
// Are 2 September 2014 and 25 September 2014 in the same year?
isSameYear(new Date(2014, 8, 2), new Date(2014, 8, 25))
// => true
// https://date-fns.org/v3.5.0/docs/isThisYear
import { isThisYear } from 'date-fns'
// If today is 25 September 2014, is 2 July 2014 in this year?
isThisYear(new Date(2014, 6, 2))
// => true
// https://date-fns.org/v3.5.0/docs/setYear
import { setYear } from 'date-fns'
// Set year 2013 to 1 September 2014:
setYear(new Date(2014, 8, 1), 2013)
// => Sun Sep 01 2013 00:00:00