Skip to content

diff‐ymd‐package API Documentation

Farhan Reza edited this page Feb 11, 2024 · 1 revision

Welcome to the diff-ymd-package wiki👋

API Documentation

DatesYMD

Represents a utility class for calculating the formatted and desired customized difference between two dates in all cases.

Create an instance of DatesYMD:

const Formatter = new DatesYMD(firstDate, secondDate);

OR for version 2.x.x and above

Can use simplified function diffDates on module-object
//const DatesYMD = require('@farhan7reza7/diff-ymd-package'); or
const DatesYMD = require('diff-ymd-package');

const Formatter = DatesYMD.diffDates(firstDate, secondDate); // can use any
  • firstDate: The first date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd' or 'yyyy.mm.dd' or dateString or dateObject or Timestamp(epoch).

  • secondDate: The second date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd' or 'yyyy.mm.dd' or dateString or dateObject or Timestamp(epoch).

  • Special case: empty string("" or '') is by default to current-date(today) for the parameters

  • Returns: An instance of DatesYMD class.

Methods:

  • diffArray()

Calculates the difference between two dates and returns an array containing Y(years), M(months), D(days), and a formatted 'aY bM cD' difference string.

const result = Formatter.diffArray();

Returns: An array containing the calculated years, months, days, and the formatted difference.

  • formattedYMD()

Returns the formatted difference between two dates in aY bM cD(aYears bMonths cDays) format.

const result = Formatter.formattedYMD();

Returns: A string in the format 'aY bM cD'.

  • customizeFormat(yearUnit, monthUnit, dayUnit, partSeparator)

Customizes the difference using specified units and separators

const result = Formatter.customizeFormat(
  yearUnit,
  monthUnit,
  dayUnit,
  partSeparator,
);

Returns: A customized formatted difference string of form (a + yearUnit + partSeparator + b + monthUnit + partSeparator + c + dayUnit), eg. aYs-bMs-cDs etc.

  • diffInMonths()

Calculates the difference in months between two dates.

const monthsDifference = Formatter.diffInMonths();

Returns: The difference in months.

  • diffInWeeks()

Calculates the difference in weeks between two dates.

const weeksDifference = Formatter.diffInWeeks();

Returns: The difference in weeks.

  • diffInDays()

Calculates the difference in days between two dates.

const daysDifference = Formatter.diffInDays();

Returns: The difference in days.

  • diffInYears()

Calculates the difference in years between two dates.

const yearsDifference = Formatter.diffInYears();

Returns: The difference in years.

  • diffInHours()

Calculates the difference in hours between two dates.

const hoursDifference = Formatter.diffInHours();

Returns: The difference in hours.

  • diffInMinutes()

Calculates the difference in minutes between two dates.

const minutesDifference = Formatter.diffInMinutes();

Returns: The difference in minutes.

  • diffInSeconds()

Calculates the difference in seconds between two dates.

const secondsDifference = Formatter.diffInSeconds();

Returns: The difference in seconds.

For more information:

See diff-ymd-package Documentation

For more information, support or help, contact us:

Farhan Reza's GitHub Farhan Reza's LinkedIn