The @dexbytes/date-helper
package is a comprehensive utility for handling date and time operations in JavaScript applications. With its intuitive API and robust functionality, it simplifies common tasks such as date validation, formatting, timezone conversion, and date arithmetic.
- Date Validation: Check the validity of date strings.
- Current UTC Date and Time: Obtain the current UTC date and time.
- Date Scheduling: Schedule dates from the current time.
- Date Formatting: Format dates according to various formats.
- Timezone Conversion: Convert dates to different timezones.
- Date Arithmetic: Add and subtract time intervals from dates.
- Date Unit Calculation: Compute the start and end of specific date units.
- Date Difference Calculation: Calculate the difference between two dates.
To use the DateHelper
class in your project, you need to install the @dexbytes/date-helper
package. You can install it via npm:
npm install @dexbytes/date-helper
You can visit our repository by using this link https://github.com/dexbytesinfotech/date-helper.git
Import the DateHelper
class into your project:
import { DateHelper, DateFormat } from '@dexbytes/date-helper';
Check if a string is a valid date:
const dateString = '2024-03-29';
if (DateHelper.isDate(dateString)) {
console.log(`${dateString} is a valid date.`);
} else {
console.log(`${dateString} is not a valid date.`);
}
// Expected output: "2024-03-29 is a valid date."
Get the current UTC date:
const utcDate = DateHelper.getUtc();
console.log('Current UTC date:', utcDate);
// Expected output: Current UTC date: [Current UTC date and time]
Get the current UTC date as a string:
const utcDateString = DateHelper.getUtcString();
console.log('Current UTC date string:', utcDateString);
// Expected output: Current UTC date string: [Current UTC date and time as a string]
Get the current UTC date in a specific format:
const formattedUtcDate = DateHelper.getUtcFormat(DateFormat.YYYY_MM_DD_HH_MM_SS);
console.log('Formatted UTC date:', formattedUtcDate);
// Expected output: Formatted UTC date: [Formatted UTC date and time according to specified format]
Schedule a date from now:
const scheduledDate = DateHelper.scheduleFromNow(1, 'days');
console.log('Scheduled date from now:', scheduledDate);
// Expected output: Scheduled date from now: [Date and time one day from now]
Format a date time:
const formattedDateTime = DateHelper.formatDateTime('2024-03-29', DateFormat.YYYY_MM_DD_HH_MM_SS);
console.log('Formatted date time:', formattedDateTime);
// Expected output: Formatted date time: [Formatted date and time string]
Format a date time without timezone:
const rawFormattedDateTime = DateHelper.formatDateTimeRaw('2024-03-29', DateFormat.YYYY_MM_DD_HH_MM_SS);
console.log('Raw formatted date time:', rawFormattedDateTime);
// Expected output: Raw formatted date time: [Raw formatted date and time string]
Get date time for a specific timezone:
const dateTimeForTimezone = DateHelper.getDateTimeForTimezone('2024-03-29', 'India/Kolkata');
console.log('Date time for timezone:', dateTimeForTimezone);
// Expected output: Date time for timezone: [Date and time adjusted to specified timezone]
Add days to a date:
const newDate = DateHelper.addDate('2024-03-29', 1, 'days');
console.log('New date after addition:', newDate);
// Expected output: New date after addition: [Date after adding one day]
Subtract days from a date:
const newDate = DateHelper.subtractDate('2024-03-29', 1, 'days');
console.log('New date after subtraction:', newDate);
// Expected output: New date after subtraction: [Date after subtracting one day]
Get the start of a date unit:
const startOfMonth = DateHelper.getStartOf('2024-03-29', 'month');
console.log('Start of month:', startOfMonth);
// Expected output: Start of month: [Start date of the month containing the given date]
Get the end of a date unit:
const endOfMonth = DateHelper.getEndOf('2024-03-29', 'month');
console.log('End of month:', endOfMonth);
// Expected output: End of month: [End date of the month containing the given date]
Calculate date difference:
const difference = DateHelper.dateDiff('2024-03-29T12:00:00', '2024-03-27T12:00:00', 'days');
console.log('Date difference:', difference);
// Expected output: Date difference: [Difference in days between the two dates]
Calculate formatted date difference:
const formattedDifference = DateHelper.dateDiffFormatted('2024-03-29T12:00:00', '2024-03-27T12:00:00', 'days');
console.log('Formatted date difference:', formattedDifference);
// Expected output: Formatted date difference: [Formatted difference in days between the two dates]
Calculate duration from now in minutes:
const durationInMinutes = DateHelper.durationFromNowInMinutes('2024-03-29');
console.log('Duration from now in minutes:', durationInMinutes);
// Expected output: Duration from now in minutes: [Duration from now to the specified date in minutes]
Calculate duration from now as a string:
const durationAsString = DateHelper.durationFromNowAsString('2024-03-29');
console.log('Duration from now as string:', durationAsString);
// Expected output: Duration from now as string: [Human-readable duration from now to the specified date]
If you have any questions about contributing to this project, feel free to reach out to us at contact@dexbytes.com.