date-helper
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.
Features
- 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.
Installation
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
Usage
Import the DateHelper
class into your project:
import { DateHelper, DateFormat } from '@dexbytes/date-helper';
isDate
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.`);
}
getUtc
Get the current UTC date:
const utcDate = DateHelper.getUtc();
console.log('Current UTC date:', utcDate);
getUtcString
Get the current UTC date as a string:
const utcDateString = DateHelper.getUtcString();
console.log('Current UTC date string:', utcDateString);
getUtcFormat
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);
scheduleFromNow
Schedule a date from now:
const scheduledDate = DateHelper.scheduleFromNow(1, 'days');
console.log('Scheduled date from now:', scheduledDate);
formatDateTime
Format a date time:
const formattedDateTime = DateHelper.formatDateTime('2024-03-29', DateFormat.YYYY_MM_DD_HH_MM_SS);
console.log('Formatted date time:', formattedDateTime);
formatDateTimeRaw
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);
getDateTimeForTimezone
Get date time for a specific timezone:
const dateTimeForTimezone = DateHelper.getDateTimeForTimezone('2024-03-29', 'India/Kolkata');
console.log('Date time for timezone:', dateTimeForTimezone);
addDate
Add days to a date:
const newDate = DateHelper.addDate('2024-03-29', 1, 'days');
console.log('New date after addition:', newDate);
subtractDate
Subtract days from a date:
const newDate = DateHelper.subtractDate('2024-03-29', 1, 'days');
console.log('New date after subtraction:', newDate);
getStartOf
Get the start of a date unit:
const startOfMonth = DateHelper.getStartOf('2024-03-29', 'month');
console.log('Start of month:', startOfMonth);
getEndOf
Get the end of a date unit:
const endOfMonth = DateHelper.getEndOf('2024-03-29', 'month');
console.log('End of month:', endOfMonth);
dateDiff
Calculate date difference:
const difference = DateHelper.dateDiff('2024-03-29T12:00:00', '2024-03-27T12:00:00', 'days');
console.log('Date difference:', difference);
dateDiffFormatted
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);
durationFromNowInMinutes
Calculate duration from now in minutes:
const durationInMinutes = DateHelper.durationFromNowInMinutes('2024-03-29');
console.log('Duration from now in minutes:', durationInMinutes);
durationFromNowAsString
Calculate duration from now as a string:
const durationAsString = DateHelper.durationFromNowAsString('2024-03-29');
console.log('Duration from now as string:', durationAsString);
Contribution
If you have any questions about contributing to this project, feel free to reach out to us at contact@dexbytes.com.