-
-
Notifications
You must be signed in to change notification settings - Fork 11
diff‐ymd‐package Usages
Farhan Reza edited this page Feb 11, 2024
·
1 revision
Welcome to the diff-ymd-package wiki👋
//const DatesYMD = require('@farhan7reza7/diff-ymd-package'); //or
const DatesYMD = require('diff-ymd-package'); // can use any
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
// 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
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