h7-date-utility
v1.0.2
Published
A TypeScript library to work with dates, time zones, and intervals. Includes methods to format, compare, and calculate business days.
Downloads
34
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.
💖 Support This Project / Apoie Este Projeto
If this project has been helpful to you, please consider making a donation to support its ongoing development. Maintaining and improving this package takes time and effort, and your contribution helps ensure we can keep delivering quality solutions to the community.
Se este projeto foi útil para você, considere fazer uma doação para apoiar seu desenvolvimento contínuo. Manter este pacote atualizado e funcional exige tempo e esforço. Sua contribuição ajuda a garantir que possamos continuar fornecendo soluções de qualidade para a comunidade.
Even a small contribution makes a big difference. Thank you for helping make this project possible!
Mesmo uma pequena contribuição faz uma grande diferença. Obrigado por ajudar a tornar este projeto possível! 🙏
Why donate? / Por que doar?
✅ To help develop new features.
Para ajudar no desenvolvimento de novos recursos.
✅ To maintain compatibility with the latest versions of libraries and tools.
Para manter a compatibilidade com as últimas versões de bibliotecas e ferramentas.
✅ To support a developer who values community contributions.
Para apoiar um desenvolvedor que acredita na contribuição com a comunidade.
📦 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.