h7-date-utility
v1.0.0
Published
A TypeScript library to work with dates, time zones, and intervals. Includes methods to format, compare, and calculate business days.
Maintainers
Readme
H7 Date Utility
H7 Date Utility is a TypeScript library that provides a set of utility functions for working with dates, time zones, and intervals. It leverages the powerful Luxon library to offer date formatting, conversion, comparison, business day calculation, and more, with full TypeScript support.
📦 Installation
To install the library, use the following command:
npm install h7-date-utility
🚀 Usage
Here's an example of how to use the DateUtility class for various date operations:
Convert Date to Time Zone
import { DateUtility } from 'h7-date-utility';
const date = "2024-11-21T00:00:00.000Z";
const timeZone = 'America/New_York';
const convertedDate = DateUtility.convertToTimeZone(date, timeZone);
console.log(convertedDate.toString());
Format Date
import { DateUtility } from 'h7-date-utility';
const date = new Date();
const format = 'yyyy LLL dd HH:mm';
const formattedDate = DateUtility.formatDate(date, format);
console.log(formattedDate);
Compare Dates
import { DateUtility } from 'h7-date-utility';
const date1 = "2024-11-21";
const date2 = "2024-11-22";
const comparison = DateUtility.compareDates(date1, date2);
if (comparison === -1) {
console.log("Date 1 is earlier than Date 2");
} else if (comparison === 1) {
console.log("Date 1 is later than Date 2");
} else {
console.log("Both dates are equal");
}
Calculate Business Days
import { DateUtility } from 'h7-date-utility';
const startDate = "2024-11-01";
const endDate = "2024-11-10";
const businessDays = DateUtility.calculateBusinessDays(startDate, endDate);
console.log(`Number of business days: ${businessDays}`);
Add/Subtract Days
import { DateUtility } from 'h7-date-utility';
const date = "2024-11-21";
const timeZone = 'America/New_York';
const newDate = DateUtility.addDays(date, 5, timeZone);
console.log(newDate.toString());
const previousDate = DateUtility.subtractDays(date, 5, timeZone);
console.log(previousDate.toString());
📖 Features
- Date Conversion: Easily convert a date to any specified time zone.
- Custom Formatting: Format dates using a custom format.
- Date Comparison: Compare two dates while considering time zones.
- Business Day Calculation: Calculate the number of business days between two dates.
- Add/Subtract Days: Add or subtract days from a given date, considering time zones.
- Luxon-powered: Leverages the power of the Luxon library for accurate date handling.
- TypeScript support: Fully typed, ensuring a seamless development experience in TypeScript projects.
- Lightweight: A simple and efficient library with no dependencies.
🛠️ How it Works
This library utilizes the Luxon library for accurate date handling. It provides methods for converting dates to specific time zones, comparing dates, calculating business days, formatting dates, and more, all while considering the time zone.
📜 Example Use Case
Imagine an application that needs to handle date and time across multiple time zones. You can use this library to manage the conversion and formatting of dates seamlessly:
import { DateUtility } from 'h7-date-utility';
const date = "2024-11-21T00:00:00.000Z";
const timeZone = 'America/New_York';
const formattedDate = DateUtility.formatDate(date, 'yyyy LLL dd HH:mm', timeZone);
console.log(`Formatted date in New York time zone: ${formattedDate}`);
🤝 Contributing
Feel free to contribute to this project by opening issues or submitting pull requests. All contributions are welcome!
👨💻 Author
Developed by Herlandio. If you find this library helpful, consider starring the repository on GitHub!
📝 License
This project is licensed under the MIT License. You are free to use, modify, and distribute it as long as you provide proper attribution.