@namopanda/date-utility
v1.0.1
Published
A date utility package with inbuilt date functions for different formats.
Downloads
4
Maintainers
Readme
Date Utilities
Date Utilities is a lightweight npm package that provides various date-related utility functions to simplify common date operations in JavaScript applications.
Installation
You can install the package via npm:
npm install @namopanda/date-utility
const {
formatDate,
calculateDaysDiff,
calculateMonthDiff,
calculateYearDiff,
convertDateFormat,
getDayOfYear,
} = require('date-utils');
// Example usage
const date = new Date();
console.log(formatDate(date)); // Output: July 5, 2023
const startDate = new Date('2023-01-01');
const endDate = new Date('2023-12-31');
console.log(calculateDaysDiff(startDate, endDate)); // Output: 364
console.log(calculateMonthDiff(startDate, endDate)); // Output: 11
console.log(calculateYearDiff(startDate, endDate)); // Output: 0
console.log(convertDateFormat(date, 'dd-mm-yyyy')); // Output: 05-07-2023
console.log(getDayOfYear(date)); // Output: 185
Available Date Utility Functions
The following date utility functions are available in this package:
formatDate(date, format): Formats a given date object into a specific format. It uses the toLocaleDateString() method with options to format the date as a string.
calculateDaysDiff(date1, date2): Calculates the number of days between two given date objects.
calculateMonthDiff(date1, date2): Calculates the number of months between two given date objects.
calculateYearDiff(date1, date2): Calculates the number of years between two given date objects.
convertDateFormat(date, format): Converts the format of a given date object to the specified format.
getDayOfYear(date): Returns the day of the year for a given date object.
Please refer to the function descriptions in the source code for more details on their usage and return values.