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/v3.5.0/docs/closestIndexTo
import { closestIndexTo } from 'date-fns'

// Which date is closer to 6 September 2015?
const dateToCompare = new Date(2015, 8, 6)
const datesArray = [
  new Date(2015, 0, 1),
  new Date(2016, 0, 1),
  new Date(2017, 0, 1),
]

closestIndexTo(dateToCompare, datesArray)
// => 1

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/v3.5.0/docs/closestTo
import { closestTo } from 'date-fns'

// Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?
const dateToCompare = new Date(2015, 8, 6)
closestTo(dateToCompare, [
  new Date(2000, 0, 1),
  new Date(2030, 0, 1),
])
// => Tue Jan 01 2030 00:00:00

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/v3.5.0/docs/compareAsc
import { compareAsc } from 'date-fns'

// Compare 11 February 1987 and 11 February 1987:
compareAsc(new Date(1987, 1, 11), new Date(1987, 1, 11))
// => 0

// Compare 11 February 1987 and 10 July 1989:
compareAsc(new Date(1987, 1, 11), new Date(1989, 6, 10))
// => -1

// Sort the array of dates:
[
  new Date(1995, 6, 2),
  new Date(1987, 1, 11),
  new Date(1989, 6, 10),
].sort(compareAsc)
// => [
//   Wed Feb 11 1987 00:00:00,
//   Mon Jul 10 1989 00:00:00,
//   Sun Jul 02 1995 00:00:00
// ]

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/v3.5.0/docs/compareDesc
import { compareDesc } from 'date-fns'

// Compare 11 February 1987 and 11 February 1987:
compareDesc(new Date(1987, 1, 11), new Date(1987, 1, 11))
// => 0

// Compare 11 February 1987 and 10 July 1989:
compareDesc(new Date(1987, 1, 11), new Date(1989, 6, 10))
// => -1

// Sort the array of dates in reverse chronological order:
[
  new Date(1995, 6, 2),
  new Date(1987, 1, 11),
  new Date(1989, 6, 10),
].sort(compareDesc)
// => [
//   Sun Jul 02 1995 00:00:00,
//   Mon Jul 10 1989 00:00:00,
//   Wed Feb 11 1987 00:00:00
// ]

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/v3.5.0/docs/isAfter
import { isAfter } from 'date-fns'

isAfter(new Date(1987, 6, 10), new Date(1989, 1, 11))
// => true

isAfter(new Date(1989, 6, 10), new Date(1989, 6, 10))
// => false

isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11))
// => true

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/v3.5.0/docs/isBefore
import { isBefore } from 'date-fns'

isBefore(new Date(1987, 6, 10), new Date(1989, 1, 11))
// => true

isBefore(new Date(1989, 6, 10), new Date(1989, 6, 10))
// => false

isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11))
// => false

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/v3.5.0/docs/isDate
import { isDate } from 'date-fns'

// For a valid date:
isDate(new Date())
// => true

// For an invalid date:
isDate(new Date(NaN))
// => true

// For some value:
isDate('2014-02-31')
// => false

// For an object:
isDate({})
// => false

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/v3.5.0/docs/isEqual
import { isEqual } from 'date-fns'

// Are 2 July 2014 06:30:45.000 and 2 July 2014 06:30:45.500 equal?
isEqual(
  new Date(2014, 6, 2, 6, 30, 45, 0),
  new Date(2014, 6, 2, 6, 30, 45, 500)
)
// => false

isEqual(
  new Date(2014, 6, 2, 6, 30, 45, 500),
  new Date(2014, 6, 2, 6, 30, 45, 500)
)
// => true

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/v3.5.0/docs/isExists
import { isExists } from 'date-fns'

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

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

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/v3.5.0/docs/isFuture
import { isFuture } from 'date-fns'

// If today is 25 September 2014, Is 31 December 2014 in the future?
isFuture(new Date(2014, 11, 31))
// => true

// If today is 25 September 2014, Is 31 December 1980 in the future?
isFuture(new Date(1980, 11, 31))
// => false

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/v3.5.0/docs/isPast
import { isPast } from 'date-fns'

//  If today is 25 September 2014, Is 2 July 2014 in the past?
isPast(new Date(2014, 6, 2))
// => true

//  If today is 25 September 2014, Is 2 July 2015 in the past?
isPast(new Date(2015, 6, 2))
// => false

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/v3.5.0/docs/isValid
import { isValid } from 'date-fns'

// For the valid date:
isValid(new Date(2014, 1, 31))
// => true

// For the value, convertable into a date:
isValid(1393804800000)
// => true

// For the invalid date:
isValid(new Date(''))
// => false

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/v3.5.0/docs/max
import { max } from 'date-fns'

// Which of these dates is the latest?
max([
  new Date(1989, 6, 10),
  new Date(1987, 1, 11),
  new Date(1995, 6, 2),
  new Date(1990, 0, 1),
])
// => Sun Jul 02 1995 00:00:00

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/v3.5.0/docs/min
import { min } from 'date-fns'

// Which of these dates is the earliest?
min([
  new Date(1989, 6, 10),
  new Date(1987, 1, 11),
  new Date(1995, 6, 2),
  new Date(1990, 0, 1),
])
// => Wed Feb 11 1987 00:00:00

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

interval

areIntervalsOverlapping

Is the given time interval overlapping with another time interval? Adjacent intervals do not count as overlapping unless inclusive is set to true.

date-fns

// https://date-fns.org/v3.5.0/docs/areIntervalsOverlapping
import { areIntervalsOverlapping } from 'date-fns'

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
)
//=> true

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
)
//=> false

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) }
)
//=> false

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
  { inclusive: true }
)
//=> true

plain js

const areIntervalsOverlapping = (
  { start: rxStart, end: rxEnd },
  { start: ryStart, end: ryEnd },
  { inclusive } = {}
) =>
  !!(
    (ryStart > rxStart && ryStart < rxEnd) ||
    (ryEnd > rxStart && ryStart < rxEnd) ||
    (inclusive && rxEnd.getTime() === ryStart.getTime()) ||
    (inclusive && ryEnd.getTime() === rxStart.getTime())
  )

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
)
//=> true

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
)
//=> false

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 20), end: new Date(2014, 0, 30) }
)
//=> false

areIntervalsOverlapping(
  { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) },
  { inclusive: true }
)
//=> true
Resources: Date#getTime

eachDayOfInterval

date-fns

// https://date-fns.org/v3.5.0/docs/eachDayOfInterval
import { eachDayOfInterval } from 'date-fns'

eachDayOfInterval({
  start: new Date(2014, 9, 6),
  end: new Date(2014, 9, 10),
})
//=> [
//   Mon Oct 06 2014 00:00:00,
//   Tue Oct 07 2014 00:00:00,
//   Wed Oct 08 2014 00:00:00,
//   Thu Oct 09 2014 00:00:00,
//   Fri Oct 10 2014 00:00:00
// ]

eachDayOfInterval(
  {
    start: new Date(2014, 9, 6),
    end: new Date(2014, 9, 10),
  },
  { step: 3 }
)

//=> [
//   Wed Oct 06 2014 00:00:00,
//   Mon Oct 09 2014 00:00:00,
// ]

plain js

const eachDayOfInterval = ({ start, end }, { step } = { step: 1 }) => {
  const retArr = []
  start.setHours(0, 0, 0, 0)
  end.setHours(0, 0, 0, 0)

  do {
    retArr.push(new Date(start.getTime()))
    start.setDate(start.getDate() + step)
  } while (start.getTime() <= end.getTime())

  return retArr
}

eachDayOfInterval({
  start: new Date(2014, 9, 6),
  end: new Date(2014, 9, 10),
})

//=> [
//   Mon Oct 06 2014 00:00:00,
//   Tue Oct 07 2014 00:00:00,
//   Wed Oct 08 2014 00:00:00,
//   Thu Oct 09 2014 00:00:00,
//   Fri Oct 10 2014 00:00:00
// ]

eachDayOfInterval(
  {
    start: new Date(2014, 9, 6),
    end: new Date(2014, 9, 10),
  },
  { step: 3 }
)
//=> [
//   Wed Oct 06 2014 00:00:00,
//   Mon Oct 08 2014 00:00:00,
// ]

isWithinInterval

date-fns

// https://date-fns.org/v3.5.0/docs/isWithinInterval
import { isWithinInterval } from 'date-fns'

isWithinInterval(new Date(2014, 0, 3), {
  start: new Date(2014, 0, 1),
  end: new Date(2014, 0, 7),
})
//=> true

isWithinInterval(new Date(2014, 0, 10), {
  start: new Date(2014, 0, 1),
  end: new Date(2014, 0, 7),
})
//=> false

const date = new Date()

isWithinInterval(date, {
  start: date,
  end: new Date(2500, 0, 1),
})
// => true

isWithinInterval(date, {
  start: new Date(2020, 0, 1),
  end: date,
})
// => true

plain js

const isWithinInterval = (date, { start, end }) =>
  date.getTime() >= start.getTime() && date.getTime() <= end.getTime()

isWithinInterval(new Date(2014, 0, 3), {
  start: new Date(2014, 0, 1),
  end: new Date(2014, 0, 7),
})
//=> true

isWithinInterval(new Date(2014, 0, 10), {
  start: new Date(2014, 0, 1),
  end: new Date(2014, 0, 7),
})
//=> false

// new Date is mocked as
// new Date(2014, 8, 25, 18, 30, 15, 500)
const date = new Date()

isWithinInterval(date, {
  start: date,
  end: new Date(2020, 0, 1),
})
// => true

isWithinInterval(date, {
  start: new Date(2000, 0, 1),
  end: date,
})
// => true

timestamp

fromUnixTime

Create a date from a Unix timestamp.

date-fns

// https://date-fns.org/v3.5.0/docs/fromUnixTime
import { fromUnixTime } from 'date-fns'

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

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/v3.5.0/docs/getTime
import { getTime } from 'date-fns'

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

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/v3.5.0/docs/getUnixTime
import { getUnixTime } from 'date-fns'

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

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/v3.5.0/docs/addMilliseconds
import { addMilliseconds } from 'date-fns'

// Add 750 milliseconds to 10 July 2014 12:45:30.000:
addMilliseconds(new Date(2014, 6, 10, 12, 45, 30, 0), 750)
// => Thu Jul 10 2014 12:45:30.750

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/v3.5.0/docs/differenceInMilliseconds
import { differenceInMilliseconds } from 'date-fns'

// How many milliseconds are between
// 2 July 2014 12:30:20.600 and 2 July 2014 12:30:21.700?
differenceInMilliseconds(
  new Date(2014, 6, 2, 12, 30, 21, 700),
  new Date(2014, 6, 2, 12, 30, 20, 600)
)
// => 1100

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/v3.5.0/docs/getMilliseconds
import { getMilliseconds } from 'date-fns'

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

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/v3.5.0/docs/setMilliseconds
import { setMilliseconds } from 'date-fns'

// Set 300 milliseconds to 1 September 2014 11:30:40.500:
setMilliseconds(new Date(2014, 8, 1, 11, 30, 40, 500), 300)
// => Mon Sep 01 2014 11:30:40.300

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/v3.5.0/docs/subMilliseconds
import { subMilliseconds } from 'date-fns'

// Subtract 750 milliseconds from 10 July 2014 12:45:30.000:
subMilliseconds(new Date(2014, 6, 10, 12, 45, 30, 0), 750)
// => Thu Jul 10 2014 12:45:29.250

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/v3.5.0/docs/addSeconds
import { addSeconds } from 'date-fns'

// Add 30 seconds to 10 July 2014 12:45:00:
addSeconds(new Date(2014, 6, 10, 12, 45, 0), 30)
// => Thu Jul 10 2014 12:45:30

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/v3.5.0/docs/differenceInSeconds
import { differenceInSeconds } from 'date-fns'

// How many seconds are between
// 2 July 2014 12:30:07.999 and 2 July 2014 12:30:20.000?
differenceInSeconds(
  new Date(2014, 6, 2, 12, 30, 20, 0),
  new Date(2014, 6, 2, 12, 30, 7, 999)
)
// => 12

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/v3.5.0/docs/endOfSecond
import { endOfSecond } from 'date-fns'

// The end of a second for 1 December 2014 22:15:45.400:
endOfSecond(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:45.999

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/v3.5.0/docs/getSeconds
import { getSeconds } from 'date-fns'

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

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/v3.5.0/docs/isSameSecond
import { isSameSecond } from 'date-fns'

// Are 4 September 2014 06:30:15.000 and 4 September 2014 06:30.15.500
// in the same second?
isSameSecond(
  new Date(2014, 8, 4, 6, 30, 15),
  new Date(2014, 8, 4, 6, 30, 15, 500)
)
// => true

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/v3.5.0/docs/isThisSecond
import { isThisSecond } from 'date-fns'

// If now is 25 September 2014 18:30:15.500,
// is 25 September 2014 18:30:15.000 in this second?
isThisSecond(new Date(2014, 8, 25, 18, 30, 15))
// => true

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/v3.5.0/docs/setSeconds
import { setSeconds } from 'date-fns'

// Set 45 seconds to 1 September 2014 11:30:40:
setSeconds(new Date(2014, 8, 1, 11, 30, 40), 45)
// => Mon Sep 01 2014 11:30:45

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/v3.5.0/docs/startOfSecond
import { startOfSecond } from 'date-fns'

// The start of a second for 1 December 2014 22:15:45.400:
startOfSecond(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:45.000

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/v3.5.0/docs/subSeconds
import { subSeconds } from 'date-fns'

// Subtract 30 seconds from 10 July 2014 12:45:00:
subSeconds(new Date(2014, 6, 10, 12, 45, 0), 30)
// => Thu Jul 10 2014 12:44:30

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/v3.5.0/docs/addMinutes
import { addMinutes } from 'date-fns'

// Add 30 minutes to 10 July 2014 12:00:00:
addMinutes(new Date(2014, 6, 10, 12, 0), 30)
// => Thu Jul 10 2014 12:30:00

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/v3.5.0/docs/differenceInMinutes
import { differenceInMinutes } from 'date-fns'

// How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
differenceInMinutes(
  new Date(2014, 6, 2, 12, 20, 0),
  new Date(2014, 6, 2, 12, 7, 59)
)
// => 12

// How many minutes are from 10:01:59 to 10:00:00
differenceInMinutes(
  new Date(2000, 0, 1, 10, 0, 0),
  new Date(2000, 0, 1, 10, 1, 59)
)
// => -1

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/v3.5.0/docs/endOfMinute
import { endOfMinute } from 'date-fns'

// The end of a minute for 1 December 2014 22:15:45.400:
endOfMinute(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:59.999

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/v3.5.0/docs/getMinutes
import { getMinutes } from 'date-fns'

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

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/v3.5.0/docs/isSameMinute
import { isSameMinute } from 'date-fns'

// Are 4 September 2014 06:30:00 and 4 September 2014 06:30:15
// in the same minute?
isSameMinute(
  new Date(2014, 8, 4, 6, 30),
  new Date(2014, 8, 4, 6, 30, 15)
)
// => true

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/v3.5.0/docs/isThisMinute
import { isThisMinute } from 'date-fns'

// If now is 25 September 2014 18:30:15.500,
// is 25 September 2014 18:30:00 in this minute?
isThisMinute(new Date(2014, 8, 25, 18, 30))
// => true

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/v3.5.0/docs/roundToNearestMinutes
import { roundToNearestMinutes } from 'date-fns'

// Round 10 July 2014 12:12:34 to nearest minute:
roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
// => Thu Jul 10 2014 12:13:00

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/v3.5.0/docs/setMinutes
import { setMinutes } from 'date-fns'

// Set 45 minutes to 1 September 2014 11:30:40:
setMinutes(new Date(2014, 8, 1, 11, 30, 40), 45)
// => Mon Sep 01 2014 11:45:40

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/v3.5.0/docs/startOfMinute
import { startOfMinute } from 'date-fns'

// The start of a minute for 1 December 2014 22:15:45.400:
startOfMinute(new Date(2014, 11, 1, 22, 15, 45, 400))
// => Mon Dec 01 2014 22:15:00

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/v3.5.0/docs/subMinutes
import { subMinutes } from 'date-fns'

// Subtract 30 minutes from 10 July 2014 12:00:00:
subMinutes(new Date(2014, 6, 10, 12, 0), 30)
// => Thu Jul 10 2014 11:30:00

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/v3.5.0/docs/addHours
import { addHours } from 'date-fns'

// Add 2 hours to 10 July 2014 23:00:00:
addHours(new Date(2014, 6, 10, 23, 0), 2)
// => Fri Jul 11 2014 01:00:00

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/v3.5.0/docs/differenceInHours
import { differenceInHours } from 'date-fns'

// How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
differenceInHours(
  new Date(2014, 6, 2, 19, 0),
  new Date(2014, 6, 2, 6, 50)
)
// => 12

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/v3.5.0/docs/endOfHour
import { endOfHour } from 'date-fns'

// The end of an hour for 2 September 2014 11:55:00:
endOfHour(new Date(2014, 8, 2, 11, 55))
// => Tue Sep 02 2014 11:59:59.999

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/v3.5.0/docs/getHours
import { getHours } from 'date-fns'

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

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/v3.5.0/docs/isSameHour
import { isSameHour } from 'date-fns'

// Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour?
isSameHour(
  new Date(2014, 8, 4, 6, 0),
  new Date(2014, 8, 4, 6, 30)
)
// => true

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/v3.5.0/docs/isThisHour
import { isThisHour } from 'date-fns'

// If now is 25 September 2014 18:30:15.500,
// is 25 September 2014 18:00:00 in this hour?
isThisHour(new Date(2014, 8, 25, 18))
// => true

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/v3.5.0/docs/setHours
import { setHours } from 'date-fns'

// Set 4 hours to 1 September 2014 11:30:00:
setHours(new Date(2014, 8, 1, 11, 30), 4)
// => Mon Sep 01 2014 04:30:00

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/v3.5.0/docs/startOfHour
import { startOfHour } from 'date-fns'

// The start of an hour for 2 September 2014 11:55:00:
startOfHour(new Date(2014, 8, 2, 11, 55))
// => Tue Sep 02 2014 11:00:00

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/v3.5.0/docs/subHours
import { subHours } from 'date-fns'

// Subtract 2 hours from 11 July 2014 01:00:00:
subHours(new Date(2014, 6, 11, 1, 0), 2)
// => Thu Jul 10 2014 23:00:00

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/v3.5.0/docs/addDays
import { addDays } from 'date-fns'

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

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/v3.5.0/docs/differenceInDays
import { differenceInDays } from 'date-fns'

// How many full days are between
// 2 July 2011 23:00:00 and 2 July 2012 00:00:00?
differenceInDays(
  new Date(2012, 6, 2, 0, 0),
  new Date(2011, 6, 2, 23, 0)
)
// => 365

// How many full days are between
// 2 July 2011 23:59:00 and 3 July 2011 00:01:00?
differenceInDays(
  new Date(2011, 6, 3, 0, 1),
  new Date(2011, 6, 2, 23, 59)
)
// => 0

// How many full days are between
// 1 March 2020 0:00 and 1 June 2020 0:00 ?
differenceInDays(new Date(2020, 5, 1), new Date(2020, 2, 1))
// => 92

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/v3.5.0/docs/getDate
import { getDate } from 'date-fns'

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

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/v3.5.0/docs/getDayOfYear
import { getDayOfYear } from 'date-fns'

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

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/v3.5.0/docs/isSameDay
import { isSameDay } from 'date-fns'

// Are 4 September 06:00:00 and 4 September 18:00:00 in the same day?
isSameDay(
  new Date(2014, 8, 4, 6, 0),
  new Date(2014, 8, 4, 18, 0)
)
// => true

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/v3.5.0/docs/isToday
import { isToday } from 'date-fns'

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

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/v3.5.0/docs/isTomorrow
import { isTomorrow } from 'date-fns'

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

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/v3.5.0/docs/isYesterday
import { isYesterday } from 'date-fns'

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

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/v3.5.0/docs/setDate
import { setDate } from 'date-fns'

// Set the 30th day of the month to 1 September 2014:
setDate(new Date(2014, 8, 1), 30)
// => Tue Sep 30 2014 00:00:00

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/v3.5.0/docs/subDays
import { subDays } from 'date-fns'

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

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/v3.5.0/docs/getDay
import { getDay } from 'date-fns'

// Which day of the week is 29 February 2012?
getDay(new Date(2012, 1, 29))
// => 3

// Which day of the week is 26 February 2012?
getDay(new Date(2012, 1, 26))
// => 0

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/v3.5.0/docs/getISODay
import { getISODay } from 'date-fns'

// Which day of the week is 29 February 2012?
getISODay(new Date(2012, 1, 29))
// => 3

// Which day of the week is 26 February 2012?
getISODay(new Date(2012, 1, 26))
// => 7

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/v3.5.0/docs/isFriday
import { isFriday } from 'date-fns'

// 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/v3.5.0/docs/isMonday
import { isMonday } from 'date-fns'

// 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/v3.5.0/docs/isSaturday
import { isSaturday } from 'date-fns'

// 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/v3.5.0/docs/isSunday
import { isSunday } from 'date-fns'

// 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/v3.5.0/docs/isThursday
import { isThursday } from 'date-fns'

// 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/v3.5.0/docs/isTuesday
import { isTuesday } from 'date-fns'

// 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/v3.5.0/docs/isWednesday
import { isWednesday } from 'date-fns'

// 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/v3.5.0/docs/isWeekend
import { isWeekend } from 'date-fns'

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

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/v3.5.0/docs/addWeeks
import { addWeeks } from 'date-fns'

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

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/v3.5.0/docs/subWeeks
import { subWeeks } from 'date-fns'

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

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/v3.5.0/docs/addMonths
import { addMonths } from 'date-fns'

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

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/v3.5.0/docs/getMonth
import { getMonth } from 'date-fns'

// 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/v3.5.0/docs/isFirstDayOfMonth
import { isFirstDayOfMonth } from 'date-fns'

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

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/v3.5.0/docs/isLastDayOfMonth
import { isLastDayOfMonth } from 'date-fns'

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

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/v3.5.0/docs/isSameMonth
import { isSameMonth } from 'date-fns'

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

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/v3.5.0/docs/isThisMonth
import { isThisMonth } from 'date-fns'

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

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

lastDayOfMonth

Get the last day of a month from date.

date-fns

// https://date-fns.org/v2.29.3/docs/lastDayOfMonth
import { lastDayOfMonth } from 'date-fns'

// Get the last day of a month.
lastDayOfMonth(new Date(2014, 1, 25)).toDateString()
// => Fri Feb 28 2014

plain js

function lastDayOfMonth(date) {
  const dateClone = new Date(date.getTime())
  const month = dateClone.getMonth()
  dateClone.setDate(1)
  dateClone.setMonth(month + 1)
  dateClone.setDate(0)

  return dateClone
}

// Get the last day of a month.
lastDayOfMonth(new Date(2014, 1, 25)).toDateString()
// => Fri Feb 28 2014

setMonth

Set the month to the given date.

date-fns

// https://date-fns.org/v3.5.0/docs/setMonth
import { setMonth } from 'date-fns'

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

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/v3.5.0/docs/subMonths
import { subMonths } from 'date-fns'

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

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/v3.5.0/docs/addYears
import { addYears } from 'date-fns'

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

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/v3.5.0/docs/differenceInCalendarYears
import { differenceInCalendarYears } from 'date-fns'

// How many calendar years are between 31 December 2013 and 11 February 2015?
differenceInCalendarYears(
  new Date(2015, 1, 11),
  new Date(2013, 11, 31)
)
// => 2

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/v3.5.0/docs/differenceInYears
import { differenceInYears } from 'date-fns'

// How many full years are between 31 December 2013 and 11 February 2015?
differenceInYears(
  new Date(2015, 1, 11),
  new Date(2013, 11, 31)
)
// => 1

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/v3.5.0/docs/getYear
import { getYear } from 'date-fns'

// 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/v3.5.0/docs/isLeapYear
import { isLeapYear } from 'date-fns'

// Is 1 September 2012 in the leap year?
isLeapYear(new Date(2012, 8, 1))
// => true

// Is 1 September 2013 in the leap year?
isLeapYear(new Date(2013, 8, 1))
// => false

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/v3.5.0/docs/isSameYear
import { isSameYear } from 'date-fns'

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

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/v3.5.0/docs/isThisYear
import { isThisYear } from 'date-fns'

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

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/v3.5.0/docs/setYear
import { setYear } from 'date-fns'

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

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
🔝