@ted2xmen/utils
v0.1.0
Published
Utility functions for React applications
Maintainers
Readme
@ted2xmen/utils
A fully type-safe utility package for React applications with date manipulation functions powered by dayjs.
Installation
npm install @ted2xmen/utils
# or
yarn add @ted2xmen/utilsUsage
import { formatDate, fromNow, addTime } from '@ted2xmen/utils';
// Format a date
const formattedDate = formatDate(new Date(), 'DD/MM/YYYY');
console.log(formattedDate); // e.g., "23/04/2025"
// Get relative time
const relativeTime = fromNow('2025-04-20');
console.log(relativeTime); // e.g., "3 days ago"
// Add time to a date
const futureDate = addTime(new Date(), 2, 'week');
console.log(futureDate); // ISO string of date 2 weeks from nowAvailable Functions
formatDate(date: DateInput, format?: string): string- Format a date using dayjsfromNow(date: DateInput): string- Get relative time (e.g., "2 hours ago")isBefore(date: DateInput, compareDate: DateInput): boolean- Check if a date is before another dateisAfter(date: DateInput, compareDate: DateInput): boolean- Check if a date is after another dateaddTime(date: DateInput, amount: number, unit: TimeUnit): string- Add time to a datesubtractTime(date: DateInput, amount: number, unit: TimeUnit): string- Subtract time from a datetoTimezone(date: DateInput, timezone: string): string- Convert a date to a specific timezonediff(date1: DateInput, date2: DateInput, unit: TimeUnit): number- Get the difference between two datesisBetween(date: DateInput, startDate: DateInput, endDate: DateInput, inclusivity?: string): boolean- Check if a date is between two other datesdayjs- Direct access to the dayjs instance with plugins initialized
Types
type DateInput = string | Date | number | dayjs.Dayjs;
type TimeUnit = 'day' | 'week' | 'month' | 'year' | 'hour' | 'minute' | 'second';License
MIT
