utilsfordate
v2.0.0
Published
Comprehensive date utility library — formatting, parsing, manipulation, diff, timezones and more
Maintainers
Readme
utilsfordate
A comprehensive, zero-dependency TypeScript date utility library.
Installation
npm install utilsfordateUsage
import { format, toISODate, addDays, fromNow, isToday } from 'utilsfordate';
format(new Date(), 'DDDD, MMMM Do YYYY'); // "Saturday, February 28th 2026"
toISODate(new Date()); // "2026-02-28"
addDays(new Date(), 7); // Date 7 days from now
fromNow('2026-01-01'); // "2 months ago"
isToday(new Date()); // trueAPI
Formatting
| Function | Example Output |
|---|---|
| format(date, pattern) | Custom token-based format |
| toISODate(date) | 2026-02-28 |
| toISODateTime(date) | 2026-02-28T14:30:00 |
| toISOFull(date) | 2026-02-28T14:30:00+05:30 |
| toUSShort(date) | 02/28/2026 |
| toEUShort(date) | 28/02/2026 |
| toUSLong(date) | February 28, 2026 |
| toEULong(date) | 28 February 2026 |
| toShortMonth(date) | Feb 28, 2026 |
| toFullWithWeekday(date) | Saturday, February 28, 2026 |
| toRFC2822(date) | Sat, 28 Feb 2026 14:30:00 +0000 |
| toTime12h(date) | 2:30 PM |
| toTimeLong(date) | 14:30:00 |
| toTimeWithMs(date) | 14:30:00.123 |
| toUnixTimestamp(date) | 1740744600 |
| toQuarter(date) | Q1 2026 |
| toISOWeek(date) | 2026-W09 |
| toLocaleString(date, locale) | Intl-based locale format |
Format Tokens
| Token | Output |
|---|---|
| YYYY / YY | 2026 / 26 |
| MMMM / MMM / MM / M | February / Feb / 02 / 2 |
| DDDD / DDD | Saturday / Sat |
| DD / D / Do | 28 / 28 / 28th |
| HH / H / hh / h | 24h / 12h variants |
| mm / ss / SSS | Minutes / Seconds / Milliseconds |
| A / a | AM / am |
| Z / ZZ | +05:30 / +0530 |
| X / x | Unix seconds / ms |
| Q / W | Quarter / ISO Week |
| [text] | Literal escape |
Manipulation
addDays(date, 5) subtractDays(date, 5)
addMonths(date, 2) subtractMonths(date, 2)
addYears(date, 1) subtractYears(date, 1)
addHours(date, 3) addMinutes(date, 30)
addWeeks(date, 2) addSeconds(date, 60)Start / End of Period
startOfDay(date) endOfDay(date)
startOfWeek(date) endOfWeek(date)
startOfMonth(date) endOfMonth(date)
startOfQuarter(date) endOfQuarter(date)
startOfYear(date) endOfYear(date)
startOfHour(date) endOfHour(date)Predicates
isToday(date) isYesterday(date) isTomorrow(date)
isPast(date) isFuture(date)
isWeekend(date) isWeekday(date)
isLeapYear(date) isValidDate(date)
isBefore(a, b) isAfter(a, b)
isSameDay(a, b) isSameMonth(a, b) isSameYear(a, b)
isBetween(date, start, end)Diff
diff(a, b) // => { years, months, weeks, days, hours, minutes, seconds, ms }
diffInDays(a, b)
diffInMonths(a, b)
diffInYears(a, b)
durationBetween(a, b) // => "3 days, 2 hours"
fromNow(date) // => "2 months ago" / "in 3 days"Parsing
parseDDMMYYYY('28/02/2026')
parseMMDDYYYY('02/28/2026')
parseYYYYMMDD('2026-02-28')
parseAny('Feb 28 2026')Getters
getAge(date)
getQuarter(date)
getWeekNumber(date)
getDayOfYear(date)
getDaysInMonth(date)
getDaysInYear(date)Timezone
toTimezone(date, 'Asia/Kolkata')
getTimezoneOffset('America/New_York')Convenience
now() // current Date
today() // start of today
tomorrow() // start of tomorrow
yesterday() // start of yesterday
min(d1, d2, d3)
max(d1, d2, d3)
clamp(date, min, max)
roundToNearestMinutes(date, 15)License
ISC
