YOU MIGHT NOT NEED DATE-FNS

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.

generic

closestIndexTo

Return an index of the closest date from the array comparing to the given date.

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

Return a date from the array closest to the given date.

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

Compare the two dates and return -1, 0 or 1.

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

Compare the two dates reverse chronologically and return -1, 0 or 1.

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

Is the first date after the second one?

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

Is the first date before the second one?

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

Is the given value a date?

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

Are the given dates equal?

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

Is the given date exists?

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

Is the given date in the future?

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

Is the given date in the past?

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

Is the given date valid?

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

Return the latest of the given dates.

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

Return the earliest of the given dates.

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

Create a date from a Unix timestamp.

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

Get the milliseconds timestamp of the given date.

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(Date.UTC(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(Date.UTC(2012, 1, 29, 11, 45, 5, 123)))
// => 1330515905123

getUnixTime

Get the seconds timestamp of the given date.

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(Date.UTC(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(Date.UTC(2012, 1, 29, 11, 45, 5)))
// => 1330512305

millisecond

addMilliseconds

Add the specified number of milliseconds to the given date.

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

Get the number of milliseconds between the given dates.

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

Get the milliseconds of the given date.

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

Set the milliseconds to the given date.

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

Subtract the specified number of milliseconds from the given date.

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

Add the specified number of seconds to the given date.

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

Get the number of seconds between the given dates.

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

Return the end of a second for the given date.

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

Get the seconds of the given date.

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

Are the given dates in the same second?

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

Is the given date in the same second as the current date?

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

Set the seconds to the given date.

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

Return the start of a second for the given date.

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

Subtract the specified number of seconds from the given date.

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

Add the specified number of minutes to the given date.

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

Get the number of minutes between the given dates.

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

Return the end of a minute for the given date.

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

Get the minutes of the given date.

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

Are the given dates in the same minute?

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

Is the given date in the same minute as the current date?

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

Rounds the given date to the nearest minute

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

Set the minutes to the given date.

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

Return the start of a minute for the given date.

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

Subtract the specified number of minutes from the given date.

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

Add the specified number of hours to the given date.

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

Get the number of hours between the given dates.

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

Return the end of an hour for the given date.

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

Get the hours of the given date.

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

Are the given dates in the same hour?

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

Is the given date in the same hour as the current date?

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

Set the hours to the given date.

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

Return the start of an hour for the given date.

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

Subtract the specified number of hours from the given date.

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

Add the specified number of days to the given date.

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

Get the number of full days between the given dates.

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

Get the day of the month of the given date.

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

Get the day of the year of the given date.

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

Are the given dates in the same day?

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

Is the given date today?

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

Is the given date tomorrow?

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

Is the given date yesterday?

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

Set the day of the month to the given date.

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

Subtract the specified number of days from the given date.

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

Get the day of the week of the given date.

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

Get the day of the ISO week of the given date.

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

Is the given date Friday?

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

Is the given date Monday? (you could also check Mondays)

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

Is the given date Saturday?

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

Is the given date Sunday?

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

Is the given date Thursday?

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

Is the given date Tuesday?

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

Is the given date Wednesday?

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

Does the given date fall on a weekend?

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

Add the specified number of weeks to the given date.

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

Subtract the specified number of weeks from the given date.

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

Add the specified number of months to the given date.

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

Get the month of the given date.

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

Is the given date the first day of a month?

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

Is the given date the last day of a month?

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

Are the given dates in the same month?

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

Is the given date in the same month as the current date?

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

Set the month to the given date.

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

Subtract the specified number of months from the given date.

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

Add the specified number of years to the given date.

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

Get the number of calendar years between the given dates.

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

Get the number of full years between the given dates.

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

Get the year of the given date.

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

Is the given date in the leap year?

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

Are the given dates in the same year?

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

Is the given date in the same year as the current date?

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

Set the year to the given date.

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