diff-ymd-package
diff-ymd-package
a javascript library
provides APIs to difference dates in formatted ways(like (aYears bMonths cDays
) or (aY bM cD
) etc., eg. age = 20Y 2M 23D
or datesDifference = 2Years 11Months 20Days
) or customized desired formats like aY-bM-cD
or aYears-bMonths-cDays
or kDays
or mWeeks
or nMonths
etc.
Table of Contents
Installation
Install from npm registry
(Preference)
npm install diff-ymd-package
Install from Github Packages registry
(require authentication)
npm install @farhan7reza7/diff-ymd-package
Steps to install from Github Packages registry
:
See steps here
Include in html page from CDN
OR
for version 2.2.1
and above
Can include in page using script tag
from CDN
<script src="https://cdn.jsdelivr.net/npm/diff-ymd-package@2.2.1/lib/index.min.js"></script>
Get any supported version script tag
Usages
Initialize:
const DatesYMD = require('diff-ymd-package');
OR
for version 2.2.1
and above
Can include in html page using script tag
from CDN
<script src="https://cdn.jsdelivr.net/npm/diff-ymd-package@2.2.1/lib/index.min.js"></script>
Create an instance:
const date1 = '2022-01-01';
const date2 = '2023-12-31';
const Formatter = new DatesYMD(date1, date2);
OR
for version 2.x.x and above
Can use simplified function diffDates
on module-object(DatesYMD here)
const date1 = '2022-01-01';
const date2 = '2023-12-31';
const Formatter = DatesYMD.diffDates(date1, date2);
Note: For CDN based, only DatesYMD constructor name creates an instance, and can use diffDates as well, but only using global DatesYMD like DatesYMD.diffDates(...)
See an example of using CDN based
Use methods to format difference:
const result = Formatter.formattedYMD();
const customizedFormat = Formatter.customizeFormat('Y', 'Ms', 'Ds', '-');
const resultArray = Formatter.diffArray();
const monthsDifference = Formatter.diffInMonths();
const weeksDifference = Formatter.diffInWeeks();
const daysDifference = Formatter.diffInDays();
const yearsDifference = Formatter.diffInYears();
const hoursDifference = Formatter.diffInHours();
const minutesDifference = Formatter.diffInMinutes();
const secondsDifference = Formatter.diffInSeconds();
Formatted Outputs:
console.log(result);
console.log(customizedFormat);
console.log(resultArray);
console.log(monthsDifference);
console.log(weeksDifference);
console.log(daysDifference);
console.log(yearsDifference);
console.log(hoursDifference);
console.log(minutesDifference);
console.log(secondsDifference);
API References
diff-ymd-package
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('diff-ymd-package');
const Formatter = DatesYMD.diffDates(firstDate, secondDate);
-
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:
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.
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.
Calculates the difference in months between two dates.
const monthsDifference = Formatter.diffInMonths();
Returns: The difference in months.
Calculates the difference in weeks between two dates.
const weeksDifference = Formatter.diffInWeeks();
Returns: The difference in weeks.
Calculates the difference in days between two dates.
const daysDifference = Formatter.diffInDays();
Returns: The difference in days.
Calculates the difference in years between two dates.
const yearsDifference = Formatter.diffInYears();
Returns: The difference in years.
Calculates the difference in hours between two dates.
const hoursDifference = Formatter.diffInHours();
Returns: The difference in hours.
Calculates the difference in minutes between two dates.
const minutesDifference = Formatter.diffInMinutes();
Returns: The difference in minutes.
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
Contributing
If you find any issues or have suggestions for improvement, please open an issue or create a pull request on the GitHub repository.
See CONTRIBUTING guidelines for more information.
Best Practices:
The code adheres to recommended practices for readability and maintainability, including:
- Meaningful variable and function names for clarity.
- Clear and concise comments to enhance understanding.
- Proper indentation and formatting for visual organization.
See mdn guidelines for more information.
License
This project is licensed under the MIT License - see the LICENSE file for details.
See License
History
For more details about what has changed in each version of this project.
See CHANGELOG