UNPKG

@keerthana_lagadapati/date-utilities

Version:

The Date Utilities Module provides a collection of reusable functions to simplify working with dates in JavaScript.

169 lines (119 loc) 4.78 kB
# Date Utilities Module The **Date Utilities Module** provides a collection of reusable functions to simplify working with dates in JavaScript. It is lightweight, consistent, and highly useful for a wide range of applications, including MERN stack projects. --- ## Features 1. **Date Formatting**: Convert date objects into specific string formats (e.g., `DD-MM-YYYY`). 2. **Add/Subtract Days**: Easily manipulate date objects by adding or subtracting days. 3. **Leap Year Check**: Determine if a given year is a leap year. 4. **Weekday Names**: Retrieve the weekday name for any date. 5. **Day Difference Calculation**: Calculate the difference in days between two dates. 6. **Weekend Check**: Check if a given date falls on a weekend. --- ## Installation To use the **Date Utilities Module** in your project, install it via npm: ```bash npm install @keerthana_lagadapati/date-utilities ``` --- ## Usage ### Importing the Module ```javascript import DateUtils from '@keerthana_lagadapati/date-utilities'; ``` ### Available Functions #### 1. `formatDate(date, format)` Formats a date object into a specified format. **Parameters:** - `date`: The date object to format. - `format`: The desired string format (default: `"DD-MM-YYYY"`). **Example:** ```javascript const today = new Date(); console.log(DateUtils.formatDate(today, "DD/MM/YYYY")); // Output: e.g., 28/01/2025 ``` --- #### 2. `addDays(date, days)` Adds a specified number of days to a date object. **Parameters:** - `date`: The base date object. - `days`: The number of days to add. **Example:** ```javascript const today = new Date(); console.log(DateUtils.addDays(today, 5)); // Output: Date 5 days from today ``` --- #### 3. `subtractDays(date, days)` Subtracts a specified number of days from a date object. **Parameters:** - `date`: The base date object. - `days`: The number of days to subtract. **Example:** ```javascript const today = new Date(); console.log(DateUtils.subtractDays(today, 5)); // Output: Date 5 days ago ``` --- #### 4. `isLeapYear(year)` Checks if a given year is a leap year. **Parameters:** - `year`: The year to check. **Example:** ```javascript console.log(DateUtils.isLeapYear(2024)); // Output: true ``` --- #### 5. `getWeekday(date)` Returns the weekday name for a given date. **Parameters:** - `date`: The date object to evaluate. **Example:** ```javascript const today = new Date(); console.log(DateUtils.getWeekday(today)); // Output: e.g., "Monday" ``` --- #### 6. `daysBetween(date1, date2)` Calculates the number of days between two dates. **Parameters:** - `date1`: The first date. - `date2`: The second date. **Example:** ```javascript const date1 = new Date('2025-01-01'); const date2 = new Date('2025-01-10'); console.log(DateUtils.daysBetween(date1, date2)); // Output: 9 ``` --- #### 7. `isWeekend(date)` Checks if a date falls on a weekend. **Parameters:** - `date`: The date object to evaluate. **Example:** ```javascript const today = new Date(); console.log(DateUtils.isWeekend(today)); // Output: true or false ``` --- ## Use Cases ### 1. Task or Event Management - **Use Case**: For a task management system or event scheduler, `addDays` and `formatDate` can help set deadlines or display events in a human-readable format. - **Example**: Automatically calculate and display task due dates. ### 2. User-Friendly Timestamps - **Use Case**: Display user activity logs (e.g., "Last login: 12-Jan-2025"). - **Example**: Use `formatDate` to show user activity in readable formats in the front-end. ### 3. Reminder System - **Use Case**: Schedule reminders for users (e.g., reminders for upcoming appointments or renewals). - **Example**: Use `addDays` to calculate future reminders and `daysBetween` to determine how soon reminders should be sent. ### 4. Analytics Dashboard - **Use Case**: Display trends over time (e.g., daily active users or revenue growth). - **Example**: Use `daysBetween` to calculate date ranges for analytics queries. ### 5. Content Expiry Handling - **Use Case**: Automatically expire content after a certain period (e.g., temporary posts or promotional offers). - **Example**: Use `subtractDays` to calculate content expiry dates in both front-end and back-end. ### 6. Localization of Weekdays - **Use Case**: Display localized weekday names in a calendar component. - **Example**: Use `getWeekday` for a user-friendly calendar in the front-end. ### 7. Leap Year Logic - **Use Case**: Handle leap-year scenarios for billing cycles or subscription periods. - **Example**: Use `isLeapYear` to accurately calculate yearly plans.