YOU MIGHT NOT NEED DATE-FNS

Just to be clear: you should use date-fns.

It’s a great library, battle tested and with a strong team, 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. This page, on the other hand, is a mere exercise to explore how to achieve similar functionalities in plain js: sometimes it’s nice to understand the nitty gritty of how stuff works behind the hood.

Some of the implementations have been simplified to make them more digestible, and they might miss edge cases covered in the original library.

On top of that there are a lot of methods yet to be ported including what’s probably the most needed: format. Please contribute on github.

generic

closestIndexTo

date-fns

// https://date-fns.org/v2.13.0/docs/closestIndexTo
const closestIndexTo = require('date-fns/closestIndexTo')

// 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

plain js

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

closestTo

date-fns

// https://date-fns.org/v2.13.0/docs/closestTo
const closestTo = require('date-fns/closestTo')

// 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

plain js

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

compareAsc

date-fns

// https://date-fns.org/v2.13.0/docs/compareAsc
const compareAsc = require('date-fns/compareAsc')

// 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
// ]

plain js

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
// ]

compareDesc

date-fns

// https://date-fns.org/v2.13.0/docs/compareDesc
const compareDesc = require('date-fns/compareDesc')

// 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
// ]

plain js

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
// ]

isAfter

date-fns

// https://date-fns.org/v2.13.0/docs/isAfter
const isAfter = require('date-fns/isAfter')

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

plain js

const isAfter = (dateA, dateB) => dateA > dateB

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

isBefore

date-fns

// https://date-fns.org/v2.13.0/docs/isBefore
const isBefore = require('date-fns/isBefore')

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

plain js

const isBefore = (dateA, dateB) => dateA < dateB

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

isDate

date-fns

// https://date-fns.org/v2.13.0/docs/isDate
const isDate = require('date-fns/isDate')

// 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

plain js

const isDate = date => date instanceof Date

// 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

isEqual

date-fns

// https://date-fns.org/v2.13.0/docs/isEqual
const isEqual = require('date-fns/isEqual')

// 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

plain js

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

isExists

date-fns

// https://date-fns.org/v2.13.0/docs/isExists
const isExists = require('date-fns/isExists')

// For the valid date:
isExists(2018, 0, 31)
// => true

// For the invalid date:
isExists(2018, 1, 31)
// => false

plain js

const isExists = (y, m, d) => {
  const date = new Date(y, m, d)

  return (
    date.getDate() === d && date.getMonth() === m && date.getFullYear() === y
  )
}

// For the valid date:
isExists(2018, 0, 31)
// => true

// For the invalid date:
isExists(2018, 1, 31)
// => false

isFuture

date-fns

// https://date-fns.org/v2.13.0/docs/isFuture
const isFuture = require('date-fns/isFuture')

// 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

plain js

const isFuture = date => date > new Date()

// Is 31 December 2214 in the future?
isFuture(new Date(2214, 11, 31))
// => true

// Is 31 December 1980 in the future?
isFuture(new Date(1980, 11, 31))
// => false

isPast

date-fns

// https://date-fns.org/v2.13.0/docs/isPast
const isPast = require('date-fns/isPast')

//  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

plain js

const isPast = date => date < new Date()

// Is 2 July 2014 in the past?
isPast(new Date(2014, 6, 2))
// => true

// Is 2 July 2214 in the past?
isPast(new Date(2214, 6, 2))
// => false

isValid

date-fns

// https://date-fns.org/v2.13.0/docs/isValid
const isValid = require('date-fns/isValid')

// 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

plain js

/* 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

max

date-fns

// https://date-fns.org/v2.13.0/docs/max
const max = require('date-fns/max')

// 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

plain js

const max = dates => new Date(Math.max(...dates.map(date => date.getTime())))

// 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

min

date-fns

// https://date-fns.org/v2.13.0/docs/min
const min = require('date-fns/min')

// 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

plain js

const min = dates => new Date(Math.min(...dates.map(date => date.getTime())))

// 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

timestamp

fromUnixTime

date-fns

// https://date-fns.org/v2.13.0/docs/fromUnixTime
const fromUnixTime = require('date-fns/fromUnixTime')

// Create the date 29 February 2012 11:45:05:
fromUnixTime(1330515905)
// => Wed Feb 29 2012 11:45:05

plain js

const fromUnixTime = unixTime => new Date(unixTime * 1000)

// Create the date 29 February 2012 11:45:05:
fromUnixTime(1330515905)
// => Wed Feb 29 2012 11:45:05

getTime

date-fns

// https://date-fns.org/v2.13.0/docs/getTime
const getTime = require('date-fns/getTime')

// Get the timestamp of 29 February 2012 11:45:05.123:
getTime(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 1330515905123

plain js

const getTime = date => date.getTime()

// Get the timestamp of 29 February 2012 11:45:05.123:
getTime(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 1330515905123

getUnixTime

date-fns

// https://date-fns.org/v2.13.0/docs/getUnixTime
const getUnixTime = require('date-fns/getUnixTime')

// Get the timestamp of 29 February 2012 11:45:05 CET:
getUnixTime(new Date(2012, 1, 29, 11, 45, 5))
// => 1330515905

plain js

const getUnixTime = date => Math.trunc(date / 1000)

// Get the timestamp of 29 February 2012 11:45:05 CET:
getUnixTime(new Date(2012, 1, 29, 11, 45, 5))
// => 1330512305

millisecond

addMilliseconds

date-fns

// https://date-fns.org/v2.13.0/docs/addMilliseconds
const addMilliseconds = require('date-fns/addMilliseconds')

// 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

plain js

const addMilliseconds = (date, ms) => {
  date.setMilliseconds(date.getMilliseconds() + ms)
  return date
}

// 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

differenceInMilliseconds

date-fns

// https://date-fns.org/v2.13.0/docs/differenceInMilliseconds
const differenceInMilliseconds = require('date-fns/differenceInMilliseconds')

// 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

plain js

const differenceInMilliseconds = (dateA, dateB) => dateA - dateB

// 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

getMilliseconds

date-fns

// https://date-fns.org/v2.13.0/docs/getMilliseconds
const getMilliseconds = require('date-fns/getMilliseconds')

// Get the milliseconds of 29 February 2012 11:45:05.123:
getMilliseconds(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 123

plain js

const getMilliseconds = date => date.getMilliseconds()

// Get the milliseconds of 29 February 2012 11:45:05.123:
getMilliseconds(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 123

setMilliseconds

date-fns

// https://date-fns.org/v2.13.0/docs/setMilliseconds
const setMilliseconds = require('date-fns/setMilliseconds')

// 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

plain js

const setMilliseconds = (date, ms) => {
  date.setMilliseconds(ms)
  return date
}

// 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

subMilliseconds

date-fns

// https://date-fns.org/v2.13.0/docs/subMilliseconds
const subMilliseconds = require('date-fns/subMilliseconds')

// 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

plain js

const subMilliseconds = (date, ms) => {
  date.setMilliseconds(date.getMilliseconds() - ms)
  return date
}

// 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

second

addSeconds

date-fns

// https://date-fns.org/v2.13.0/docs/addSeconds
const addSeconds = require('date-fns/addSeconds')

// 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

plain js

const addSeconds = (date, s) => {
  date.setSeconds(date.getSeconds() + s)
  return date
}

// 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

differenceInSeconds

date-fns

// https://date-fns.org/v2.13.0/docs/differenceInSeconds
const differenceInSeconds = require('date-fns/differenceInSeconds')

// 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

plain js

const differenceInSeconds = (dateA, dateB) => Math.trunc((dateA - dateB) / 1000)

// 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

endOfSecond

date-fns

// https://date-fns.org/v2.13.0/docs/endOfSecond
const endOfSecond = require('date-fns/endOfSecond')

// 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

plain js

const endOfSecond = date => new Date(Math.ceil(date / 1000) * 1000 - 1)

// 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

getSeconds

date-fns

// https://date-fns.org/v2.13.0/docs/getSeconds
const getSeconds = require('date-fns/getSeconds')

// Get the seconds of 29 February 2012 11:45:05.123:
getSeconds(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 5

plain js

const getSeconds = date => date.getSeconds()

// Get the seconds of 29 February 2012 11:45:05.123:
getSeconds(new Date(2012, 1, 29, 11, 45, 5, 123))
// => 5

isSameSecond

date-fns

// https://date-fns.org/v2.13.0/docs/isSameSecond
const isSameSecond = require('date-fns/isSameSecond')

// 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

plain js

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

isThisSecond

date-fns

// https://date-fns.org/v2.13.0/docs/isThisSecond
const isThisSecond = require('date-fns/isThisSecond')

// 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

plain js

const isThisSecond = (dateA, dateB = new Date()) =>
  Math.abs(dateA - dateB) < 1000 && dateA.getSeconds() === dateB.getSeconds()

// 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

setSeconds

date-fns

// https://date-fns.org/v2.13.0/docs/setSeconds
const setSeconds = require('date-fns/setSeconds')

// 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

plain js

const setSeconds = (date, s) => {
  date.setSeconds(s)
  return date
}

// 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

startOfSecond

date-fns

// https://date-fns.org/v2.13.0/docs/startOfSecond
const startOfSecond = require('date-fns/startOfSecond')

// 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

plain js

const startOfSecond = date => new Date(Math.floor(date / 1000) * 1000)

// 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

subSeconds

date-fns

// https://date-fns.org/v2.13.0/docs/subSeconds
const subSeconds = require('date-fns/subSeconds')

// 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

plain js

const subSeconds = (date, s) => {
  date.setSeconds(date.getSeconds() - s)
  return date
}

// 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

minute

addMinutes

date-fns

// https://date-fns.org/v2.13.0/docs/addMinutes
const addMinutes = require('date-fns/addMinutes')

// 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

plain js

const addMinutes = (date, m) => {
  date.setMinutes(date.getMinutes() + m)
  return date
}

// 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

differenceInMinutes

date-fns

// https://date-fns.org/v2.13.0/docs/differenceInMinutes
const differenceInMinutes = require('date-fns/differenceInMinutes')

// 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

plain js

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

endOfMinute

date-fns

// https://date-fns.org/v2.13.0/docs/endOfMinute
const endOfMinute = require('date-fns/endOfMinute')

// 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

plain js

const endOfMinute = date =>
  new Date(Math.ceil(date / (1000 * 60)) * 1000 * 60 - 1)

// 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

getMinutes

date-fns

// https://date-fns.org/v2.13.0/docs/getMinutes
const getMinutes = require('date-fns/getMinutes')

// Get the minutes of 29 February 2012 11:45:05:
getMinutes(new Date(2012, 1, 29, 11, 45, 5))
// => 45

plain js

const getMinutes = date => date.getMinutes()

// Get the minutes of 29 February 2012 11:45:05:
getMinutes(new Date(2012, 1, 29, 11, 45, 5))
// => 45

isSameMinute

date-fns

// https://date-fns.org/v2.13.0/docs/isSameMinute
const isSameMinute = require('date-fns/isSameMinute')

// 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

plain js

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

isThisMinute

date-fns

// https://date-fns.org/v2.13.0/docs/isThisMinute
const isThisMinute = require('date-fns/isThisMinute')

// 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

plain js

const isThisMinute = (dateA, dateB = new Date()) =>
  Math.abs(dateA - dateB) < 1000 * 60 &&
  dateA.getMinutes() === dateB.getMinutes()

// 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

roundToNearestMinutes

date-fns

// https://date-fns.org/v2.13.0/docs/roundToNearestMinutes
const roundToNearestMinutes = require('date-fns/roundToNearestMinutes')

// 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

plain js

const roundToNearestMinutes = date =>
  new Date(Math.round(date / (1000 * 60)) * 1000 * 60)

// 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

setMinutes

date-fns

// https://date-fns.org/v2.13.0/docs/setMinutes
const setMinutes = require('date-fns/setMinutes')

// 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

plain js

const setMinutes = (date, m) => {
  date.setMinutes(m)
  return date
}

// 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

startOfMinute

date-fns

// https://date-fns.org/v2.13.0/docs/startOfMinute
const startOfMinute = require('date-fns/startOfMinute')

// 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

plain js

const startOfMinute = date =>
  new Date(Math.floor(date / (1000 * 60)) * 1000 * 60)

// 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

subMinutes

date-fns

// https://date-fns.org/v2.13.0/docs/subMinutes
const subMinutes = require('date-fns/subMinutes')

// 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

plain js

const subMinutes = (date, m) => {
  date.setMinutes(date.getMinutes() - m)
  return date
}

// 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

hour

addHours

date-fns

// https://date-fns.org/v2.13.0/docs/addHours
const addHours = require('date-fns/addHours')

// 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

plain js

const addHours = (date, h) => {
  date.setHours(date.getHours() + h)
  return date
}

// 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

differenceInHours

date-fns

// https://date-fns.org/v2.13.0/docs/differenceInHours
const differenceInHours = require('date-fns/differenceInHours')

// 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

plain js

const HOUR_IN_MS = 1000 * 60 * 60

const differenceInHours = (dateA, dateB) =>
  Math.trunc((dateA - dateB) / HOUR_IN_MS)

// 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

endOfHour

date-fns

// https://date-fns.org/v2.13.0/docs/endOfHour
const endOfHour = require('date-fns/endOfHour')

// 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

plain js

const HOUR_IN_MS = 1000 * 60 * 60

const endOfHour = date =>
  new Date(Math.ceil(date.getTime() / HOUR_IN_MS) * HOUR_IN_MS - 1)

// 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

getHours

date-fns

// https://date-fns.org/v2.13.0/docs/getHours
const getHours = require('date-fns/getHours')

// Get the hours of 29 February 2012 11:45:00:
getHours(new Date(2012, 1, 29, 11, 45))
// => 11

plain js

const getHours = date => date.getHours()

// Get the hours of 29 February 2012 11:45:00:
getHours(new Date(2012, 1, 29, 11, 45))
// => 11

isSameHour

date-fns

// https://date-fns.org/v2.13.0/docs/isSameHour
const isSameHour = require('date-fns/isSameHour')

// 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

plain js

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

isThisHour

date-fns

// https://date-fns.org/v2.13.0/docs/isThisHour
const isThisHour = require('date-fns/isThisHour')

// 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

plain js

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

setHours

date-fns

// https://date-fns.org/v2.13.0/docs/setHours
const setHours = require('date-fns/setHours')

// 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

plain js

const setHours = (date, h) => {
  date.setHours(h)
  return date
}

// 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

startOfHour

date-fns

// https://date-fns.org/v2.13.0/docs/startOfHour
const startOfHour = require('date-fns/startOfHour')

// 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

plain js

const HOUR_IN_MS = 1000 * 60 * 60

const startOfHour = date => new Date(Math.floor(date / HOUR_IN_MS) * HOUR_IN_MS)

// 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

subHours

date-fns

// https://date-fns.org/v2.13.0/docs/subHours
const subHours = require('date-fns/subHours')

// 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

plain js

const subHours = (date, h) => {
  date.setHours(date.getHours() - h)
  return date
}

// 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

day

addDays

date-fns

// https://date-fns.org/v2.13.0/docs/addDays
const addDays = require('date-fns/addDays')

// Add 10 days to 1 September 2014:
addDays(new Date(2014, 8, 1), 10)
// => Thu Sep 11 2014 00:00:00

plain js

const addDays = (date, d) => {
  date.setDate(date.getDate() + d)
  return date
}

// Add 10 days to 1 September 2014:
addDays(new Date(2014, 8, 1), 10)
// => Thu Sep 11 2014 00:00:00

differenceInDays

date-fns

// https://date-fns.org/v2.13.0/docs/differenceInDays
const differenceInDays = require('date-fns/differenceInDays')

// 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

plain js

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

getDate

date-fns

// https://date-fns.org/v2.13.0/docs/getDate
const getDate = require('date-fns/getDate')

// Which day of the month is 29 February 2012?
getDate(new Date(2012, 1, 29))
// => 29

plain js

const getDate = date => date.getDate()

// Which day of the month is 29 February 2012?
getDate(new Date(2012, 1, 29))
// => 29

getDayOfYear

date-fns

// https://date-fns.org/v2.13.0/docs/getDayOfYear
const getDayOfYear = require('date-fns/getDayOfYear')

// Which day of the year is 2 July 2014?
getDayOfYear(new Date(2014, 6, 2))
// => 183

plain js

const DAY_IN_MS = 1000 * 60 * 60 * 24
const getDayOfYear = date =>
  (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) -
    Date.UTC(date.getFullYear(), 0, 0)) /
  DAY_IN_MS

// Which day of the year is 2 July 2014?
getDayOfYear(new Date(2014, 6, 2))
// => 183

isSameDay

date-fns

// https://date-fns.org/v2.13.0/docs/isSameDay
const isSameDay = require('date-fns/isSameDay')

// 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

plain js

const isSameDay = (dateA, dateB) =>
  dateA.toLocaleDateString() === dateB.toLocaleDateString()

// 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

isToday

date-fns

// https://date-fns.org/v2.13.0/docs/isToday
const isToday = require('date-fns/isToday')

// If today is 25 September 2014, is 25 September 14:00 today?
isToday(new Date(2014, 8, 25, 14, 0))
// => true

plain js

const isToday = date => {
  const yesterday = new Date()
  return yesterday.toLocaleDateString() === date.toLocaleDateString()
}

// If today is 25 September 2014, is 25 September 14:00 today?
isToday(new Date(2014, 8, 25, 14, 0))
// => true

isTomorrow

date-fns

// https://date-fns.org/v2.13.0/docs/isTomorrow
const isTomorrow = require('date-fns/isTomorrow')

// If today is 25 September 2014, is 24 September tomorrow?
isTomorrow(new Date(2014, 8, 26, 14, 0))
// => true

plain js

const isTomorrow = date => {
  const yesterday = new Date()
  yesterday.setDate(yesterday.getDate() + 1)
  return yesterday.toLocaleDateString() === date.toLocaleDateString()
}

// If today is 25 September 2014, is 24 September tomorrow?
isTomorrow(new Date(2014, 8, 26, 14, 0))
// => true

isYesterday

date-fns

// https://date-fns.org/v2.13.0/docs/isYesterday
const isYesterday = require('date-fns/isYesterday')

// If today is 25 September 2014, is 24 September yesterday?
isYesterday(new Date(2014, 8, 24, 14, 0))
// => true

plain js

const isYesterday = date => {
  const yesterday = new Date()
  yesterday.setDate(yesterday.getDate() - 1)
  return yesterday.toLocaleDateString() === date.toLocaleDateString()
}

// If today is 25 September 2014, is 24 September yesterday?
isYesterday(new Date(2014, 8, 24, 14, 0))
// => true

setDate

date-fns

// https://date-fns.org/v2.13.0/docs/setDate
const setDate = require('date-fns/setDate')

// 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

plain js

const setDate = (date, d) => {
  date.setDate(d)
  return date
}

// 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

subDays

date-fns

// https://date-fns.org/v2.13.0/docs/subDays
const subDays = require('date-fns/subDays')

// Subtract 10 days from 1 September 2014:
subDays(new Date(2014, 8, 1), 10)
// => Fri Aug 22 2014 00:00:00

plain js

const subDays = (date, d) => {
  date.setDate(date.getDate() - d)
  return date
}

// Subtract 10 days from 1 September 2014:
subDays(new Date(2014, 8, 1), 10)
// => Fri Aug 22 2014 00:00:00

weekday

getDay

date-fns

// https://date-fns.org/v2.13.0/docs/getDay
const getDay = require('date-fns/getDay')

// 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

plain js

const getDay = date => date.getDay()

// 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

getISODay

date-fns

// https://date-fns.org/v2.13.0/docs/getISODay
const getISODay = require('date-fns/getISODay')

// 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

plain js

const getISODay = date => date.getDay() || 7

// 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

isFriday

date-fns

// https://date-fns.org/v2.13.0/docs/isFriday
const isFriday = require('date-fns/isFriday')

// Is 26 September 2014 Friday?
isFriday(new Date(2014, 8, 26))
// => true

plain js

const isFriday = date => date.getDay() === 5

// Is 26 September 2014 Friday?
isFriday(new Date(2014, 8, 26))
// => true

isMonday

date-fns

// https://date-fns.org/v2.13.0/docs/isMonday
const isMonday = require('date-fns/isMonday')

// Is 22 September 2014 Monday?
isMonday(new Date(2014, 8, 22))
// => true

plain js

const isMonday = date => date.getDay() === 1

// Is 22 September 2014 Monday?
isMonday(new Date(2014, 8, 22))
// => true

isSaturday

date-fns

// https://date-fns.org/v2.13.0/docs/isSaturday
const isSaturday = require('date-fns/isSaturday')

// Is 27 September 2014 Saturday?
isSaturday(new Date(2014, 8, 27))
// => true

plain js

const isSaturday = date => date.getDay() === 6

// Is 27 September 2014 Saturday?
isSaturday(new Date(2014, 8, 27))
// => true

isSunday

date-fns

// https://date-fns.org/v2.13.0/docs/isSunday
const isSunday = require('date-fns/isSunday')

// Is 21 September 2014 Sunday?
isSunday(new Date(2014, 8, 21))
// => true

plain js

const isSunday = date => date.getDay() === 0

// Is 21 September 2014 Sunday?
isSunday(new Date(2014, 8, 21))
// => true

isThursday

date-fns

// https://date-fns.org/v2.13.0/docs/isThursday
const isThursday = require('date-fns/isThursday')

// Is 25 September 2014 Thursday?
isThursday(new Date(2014, 8, 25))
// => true

plain js

const isThursday = date => date.getDay() === 4

// Is 25 September 2014 Thursday?
isThursday(new Date(2014, 8, 25))
// => true

isTuesday

date-fns

// https://date-fns.org/v2.13.0/docs/isTuesday
const isTuesday = require('date-fns/isTuesday')

// Is 23 September 2014 Tuesday?
isTuesday(new Date(2014, 8, 23))
// => true

plain js

const isTuesday = date => date.getDay() === 2

// Is 23 September 2014 Tuesday?
isTuesday(new Date(2014, 8, 23))
// => true

isWednesday

date-fns

// https://date-fns.org/v2.13.0/docs/isWednesday
const isWednesday = require('date-fns/isWednesday')

// Is 24 September 2014 Wednesday?
isWednesday(new Date(2014, 8, 24))
// => true

plain js

const isWednesday = date => date.getDay() === 3

// Is 24 September 2014 Wednesday?
isWednesday(new Date(2014, 8, 24))
// => true

isWeekend

date-fns

// https://date-fns.org/v2.13.0/docs/isWeekend
const isWeekend = require('date-fns/isWeekend')

// Does 5 October 2014 fall on a weekend?
isWeekend(new Date(2014, 9, 5))
// => true

plain js

const isWeekend = date => [0, 6].includes(date.getDay())

// Does 5 October 2014 fall on a weekend?
isWeekend(new Date(2014, 9, 5))
// => true

week

addWeeks

date-fns

// https://date-fns.org/v2.13.0/docs/addWeeks
const addWeeks = require('date-fns/addWeeks')

// Add 4 weeks to 1 September 2014:
addWeeks(new Date(2014, 8, 1), 4)
// => Mon Sep 29 2014 00:00:00

plain js

const addWeeks = (date, w) => {
  date.setDate(date.getDate() + w * 7)
  return date
}

// Add 4 weeks to 1 September 2014:
addWeeks(new Date(2014, 8, 1), 4)
// => Mon Sep 29 2014 00:00:00

subWeeks

date-fns

// https://date-fns.org/v2.13.0/docs/subWeeks
const subWeeks = require('date-fns/subWeeks')

// Subtract 4 weeks from 1 September 2014:
subWeeks(new Date(2014, 8, 1), 4)
// => Mon Aug 04 2014 00:00:00

plain js

const subWeeks = (date, w) => {
  date.setDate(date.getDate() - w * 7)
  return date
}

// Subtract 4 weeks from 1 September 2014:
subWeeks(new Date(2014, 8, 1), 4)
// => Mon Aug 04 2014 00:00:00

month

addMonths

date-fns

// https://date-fns.org/v2.13.0/docs/addMonths
const addMonths = require('date-fns/addMonths')

// Add 5 months to 1 September 2014:
addMonths(new Date(2014, 8, 1), 5)
// => Sun Feb 01 2015 00:00:00

plain js

const addMonths = (date, m) => {
  date.setMonth(date.getMonth() + m)
  return date
}

// Add 5 months to 1 September 2014:
addMonths(new Date(2014, 8, 1), 5)
// => Sun Feb 01 2015 00:00:00

getMonth

date-fns

// https://date-fns.org/v2.13.0/docs/getMonth
const getMonth = require('date-fns/getMonth')

// Which month is 29 February 2012?
getMonth(new Date(2012, 1, 29))
// => 1

plain js

const getMonth = date => date.getMonth()

// Which month is 29 February 2012?
getMonth(new Date(2012, 1, 29))
// => 1

isFirstDayOfMonth

date-fns

// https://date-fns.org/v2.13.0/docs/isFirstDayOfMonth
const isFirstDayOfMonth = require('date-fns/isFirstDayOfMonth')

// Is 1 September 2014 the first day of a month?
isFirstDayOfMonth(new Date(2014, 8, 1))
// => true

plain js

const isFirstDayOfMonth = date => date.getDate() === 1

// Is 1 September 2014 the first day of a month?
isFirstDayOfMonth(new Date(2014, 8, 1))
// => true

isLastDayOfMonth

date-fns

// https://date-fns.org/v2.13.0/docs/isLastDayOfMonth
const isLastDayOfMonth = require('date-fns/isLastDayOfMonth')

// Is 28 February 2014 the last day of a month?
isLastDayOfMonth(new Date(2014, 1, 28))
// => true

plain js

const isLastDayOfMonth = date => {
  const dateClone = new Date(date.getTime())
  dateClone.setDate(date.getDate() + 1)
  return date.getMonth() === dateClone.getMonth() - 1
}

// Is 28 February 2014 the last day of a month?
isLastDayOfMonth(new Date(2014, 1, 28))
// => true

isSameMonth

date-fns

// https://date-fns.org/v2.13.0/docs/isSameMonth
const isSameMonth = require('date-fns/isSameMonth')

// Are 2 September 2014 and 25 September 2014 in the same month?
isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25))
// => true

plain js

const isSameMonth = (dateA, dateB) =>
  dateA.getYear() === dateB.getYear() && dateA.getMonth() === dateB.getMonth()

// Are 2 September 2014 and 25 September 2014 in the same month?
isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25))
// => true

isThisMonth

date-fns

// https://date-fns.org/v2.13.0/docs/isThisMonth
const isThisMonth = require('date-fns/isThisMonth')

// If today is 25 September 2014, is 15 September 2014 in this month?
isThisMonth(new Date(2014, 8, 15))
// => true

plain js

const isThisMonth = date => {
  const now = new Date()
  return date.getYear() === now.getYear() && date.getMonth() === now.getMonth()
}

// If today is 25 September 2014, is 15 September 2014 in this month?
isThisMonth(new Date(2014, 8, 15))
// => true

setMonth

date-fns

// https://date-fns.org/v2.13.0/docs/setMonth
const setMonth = require('date-fns/setMonth')

// Set February to 1 September 2014:
setMonth(new Date(2014, 8, 1), 1)
// => Sat Feb 01 2014 00:00:00

plain js

const setMonth = (date, m) => {
  date.setMonth(m)
  return date
}

// Set February to 1 September 2014:
setMonth(new Date(2014, 8, 1), 1)
// => Sat Feb 01 2014 00:00:00

subMonths

date-fns

// https://date-fns.org/v2.13.0/docs/subMonths
const subMonths = require('date-fns/subMonths')

// Subtract 5 months from 1 February 2015:
subMonths(new Date(2015, 1, 1), 5)
// => Mon Sep 01 2014 00:00:00

plain js

const subMonths = (date, m) => {
  date.setMonth(date.getMonth() - m)
  return date
}

// Subtract 5 months from 1 February 2015:
subMonths(new Date(2015, 1, 1), 5)
// => Mon Sep 01 2014 00:00:00

year

addYears

date-fns

// https://date-fns.org/v2.13.0/docs/addYears
const addYears = require('date-fns/addYears')

// Add 5 years to 1 September 2014:
addYears(new Date(2014, 8, 1), 5)
// => Sun Sep 01 2019 00:00:00

plain js

const addYears = (date, yearsToAdd) => {
  date.setFullYear(date.getFullYear() + yearsToAdd)
  return date
}

// Add 5 years to 1 September 2014:
addYears(new Date(2014, 8, 1), 5)
// => Sun Sep 01 2019 00:00:00

differenceInCalendarYears

date-fns

// https://date-fns.org/v2.13.0/docs/differenceInCalendarYears
const differenceInCalendarYears = require('date-fns/differenceInCalendarYears')

// 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

plain js

const differenceInCalendarYears = (dateA, dateB) =>
  Math.abs(dateA.getFullYear() - dateB.getFullYear())

// 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

differenceInYears

date-fns

// https://date-fns.org/v2.13.0/docs/differenceInYears
const differenceInYears = require('date-fns/differenceInYears')

// 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

plain js

const YEAR_IN_MS = 1000 * 60 * 60 * 24 * 365

const differenceInYears = (dateA, dateB) =>
  Math.floor((dateA - dateB) / YEAR_IN_MS)

// 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

getYear

date-fns

// https://date-fns.org/v2.13.0/docs/getYear
const getYear = require('date-fns/getYear')

// Which year is 2 July 2014?
getYear(new Date(2014, 6, 2))
// => 2014

plain js

const getYear = date => date.getFullYear()

// Which year is 2 July 2014?
getYear(new Date(2014, 6, 2))
// => 2014

isLeapYear

date-fns

// https://date-fns.org/v2.13.0/docs/isLeapYear
const isLeapYear = require('date-fns/isLeapYear')

// 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

plain js

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

isSameYear

date-fns

// https://date-fns.org/v2.13.0/docs/isSameYear
const isSameYear = require('date-fns/isSameYear')

// Are 2 September 2014 and 25 September 2014 in the same year?
isSameYear(new Date(2014, 8, 2), new Date(2014, 8, 25))
// => true

plain js

const isSameYear = (dateA, dateB) => dateA.getFullYear() === dateB.getFullYear()

// Are 2 September 2014 and 25 September 2014 in the same year?
isSameYear(new Date(2014, 8, 2), new Date(2014, 8, 25))
// => true

isThisYear

date-fns

// https://date-fns.org/v2.13.0/docs/isThisYear
const isThisYear = require('date-fns/isThisYear')

// If today is 25 September 2014, is 2 July 2014 in this year?
isThisYear(new Date(2014, 6, 2))
// => true

plain js

const isThisYear = dateA => dateA.getFullYear() === new Date().getFullYear()

isThisYear(new Date())
// => true

setYear

date-fns

// https://date-fns.org/v2.13.0/docs/setYear
const setYear = require('date-fns/setYear')

// Set year 2013 to 1 September 2014:
setYear(new Date(2014, 8, 1), 2013)
// => Sun Sep 01 2013 00:00:00

plain js

const setYear = (date, year) => {
  date.setFullYear(year)
  return date
}

// Set year 2013 to 1 September 2014:
setYear(new Date(2014, 8, 1), 2013)
// => Sun Sep 01 2013 00:00:00