Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/datetime/mod.ts>difference

The Deno Standard Library
Go to Latest
function difference
import { difference } from "https://deno.land/std@0.222.1/datetime/mod.ts";

Calculates the difference of the 2 given dates in the given units. If the units are omitted, it returns the difference in the all available units.

Examples

Basic usage

import { difference } from "https://deno.land/std@0.222.1/datetime/difference.ts";

const date0 = new Date("2018-05-14");
const date1 = new Date("2020-05-13");

difference(date0, date1);
// {
//   milliseconds: 63072000000,
//   seconds: 63072000,
//   minutes: 1051200,
//   hours: 17520,
//   days: 730,
//   weeks: 104,
//   months: 23,
//   quarters: 7,
//   years: 1
// }

Calculate difference in specific units

import { difference } from "https://deno.land/std@0.222.1/datetime/difference.ts";

const date0 = new Date("2018-05-14");
const date1 = new Date("2020-05-13");

difference(date0, date1, { units: ["days", "months", "years"] });
// { days: 730, months: 23, years: 1 }

Parameters

from: Date

Year to calculate difference from.

to: Date

Year to calculate difference to.

optional
options: DifferenceOptions

Options such as units to calculate difference in.