diff-ymd-package 2.3.2
Install from the command line:
Learn more about npm packages
$ npm install @farhan7reza7/diff-ymd-package@2.3.2
Install via package.json:
"@farhan7reza7/diff-ymd-package": "2.3.2"
About this version
diff-ymd-package
ajavascript 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 likeaY-bM-cD
oraYears-bMonths-cDays
orkDays
ormWeeks
ornMonths
etc.
npm install diff-ymd-package
npm install @farhan7reza7/diff-ymd-package
OR
for version 2.2.1
and above
Can include in page using script tag
from CDN
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js"></script>
Get any supported version script tag
//const DatesYMD = require('@farhan7reza7/diff-ymd-package'); //or
const DatesYMD = require('diff-ymd-package'); // can use any if authenticated
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/[email protected]/lib/index.min.js"></script>
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); // can use any
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
// format output in aY bM cD format
const result = Formatter.formattedYMD(); // Output: "1Y 11M 30D"
// create format of your choice
const customizedFormat = Formatter.customizeFormat('Y', 'Ms', 'Ds', '-'); // Output: "1Y-11Ms-30Ds"
// return an array having years, months, days, and the formatted difference
const resultArray = Formatter.diffArray(); // Output: [1, 11, 30, '1Y 11M 30D']
// Calculate the difference in months
const monthsDifference = Formatter.diffInMonths(); // Output: 23
// Calculate the difference in weeks
const weeksDifference = Formatter.diffInWeeks(); // Output: 104
// Calculate the difference in days
const daysDifference = Formatter.diffInDays(); // Output: 729
// Calculate the difference in years
const yearsDifference = Formatter.diffInYears(); // Output: 1
// Calculate the difference in hours
const hoursDifference = Formatter.diffInHours(); // Output: 17496
// Calculate the difference in minutes
const minutesDifference = Formatter.diffInMinutes(); // Output: 1049760
// Calculate the difference in seconds
const secondsDifference = Formatter.diffInSeconds(); // Output: 62985600
Get Built in codes
for using
diff-ymd-package any Method
on created instance(Formatter)
console.log(result); // Output: "1Y 11M 30D"
// formatted output in aY bM cD format
console.log(customizedFormat); // Output: "1Y-11Ms-30Ds"
// you can use this method for creating format of your choice
console.log(resultArray); // Output: [1, 11, 30, '1Y 11M 30D']
/* you can access each of Y, M, D separately from output array and can format as per your choice like aY-bM-cD or aYears-bMonths-cDays etc.*/
/*example:
let Y, M, D;
Y = resultArray[0];
M = resultArray[1];
D = resultArray[2];
const customFormat = Y + 'year ' + M + 'months ' + D + 'days';
console.log(customFormat); // output: 1year 11months 30days
*/
console.log(monthsDifference); // Output: 23
// Calculate the difference in months
console.log(weeksDifference); // Output: 104
// Calculate the difference in weeks
console.log(daysDifference); // Output: 729
// Calculate the difference in days
console.log(yearsDifference); // Output: 1
// Calculate the difference in years
console.log(hoursDifference); // Output: 17496
// Calculate the difference in hours
console.log(minutesDifference); // Output: 1049760
// Calculate the difference in minutes
console.log(secondsDifference); // Output: 62985600
// Calculate the difference in seconds
diff-ymd-package
- Classes
Represents a utility class for calculating the formatted and desired customized difference between two dates in all cases.
const Formatter = new DatesYMD(firstDate, secondDate);
//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.
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'.
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.
See diff-ymd-package Documentation
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.
- 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.
This project is licensed under the MIT License - see the LICENSE file for details.
For more details about what has changed in each version of this project.