@keerthana_lagadapati/date-utilities
v1.0.1
Published
The Date Utilities Module provides a collection of reusable functions to simplify working with dates in JavaScript.
Maintainers
Readme
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
- Date Formatting: Convert date objects into specific string formats (e.g.,
DD-MM-YYYY). - Add/Subtract Days: Easily manipulate date objects by adding or subtracting days.
- Leap Year Check: Determine if a given year is a leap year.
- Weekday Names: Retrieve the weekday name for any date.
- Day Difference Calculation: Calculate the difference in days between two dates.
- 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:
npm install @keerthana_lagadapati/date-utilitiesUsage
Importing the Module
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:
const today = new Date();
console.log(DateUtils.formatDate(today, "DD/MM/YYYY")); // Output: e.g., 28/01/20252. 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:
const today = new Date();
console.log(DateUtils.addDays(today, 5)); // Output: Date 5 days from today3. 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:
const today = new Date();
console.log(DateUtils.subtractDays(today, 5)); // Output: Date 5 days ago4. isLeapYear(year)
Checks if a given year is a leap year.
Parameters:
year: The year to check.
Example:
console.log(DateUtils.isLeapYear(2024)); // Output: true5. getWeekday(date)
Returns the weekday name for a given date.
Parameters:
date: The date object to evaluate.
Example:
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:
const date1 = new Date('2025-01-01');
const date2 = new Date('2025-01-10');
console.log(DateUtils.daysBetween(date1, date2)); // Output: 97. isWeekend(date)
Checks if a date falls on a weekend.
Parameters:
date: The date object to evaluate.
Example:
const today = new Date();
console.log(DateUtils.isWeekend(today)); // Output: true or falseUse Cases
1. Task or Event Management
- Use Case: For a task management system or event scheduler,
addDaysandformatDatecan 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
formatDateto 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
addDaysto calculate future reminders anddaysBetweento 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
daysBetweento 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
subtractDaysto 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
getWeekdayfor 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
isLeapYearto accurately calculate yearly plans.
