xkernel
v0.1.0
Published
A lightweight TypeScript library providing utility functions for date manipulation and formatting.
Maintainers
Readme
xkernel
A lightweight TypeScript library providing utility functions for date manipulation, formatting, comparison, and more.
Features
- 🕐 Formatting — Flexible date formatting with token-based templates
- ➕ Calculation — Add/subtract time units (years, months, days, hours, etc.)
- 🔍 Comparison — Compare dates, check if same day/month/year, relative time checks
- 📏 Diff — Calculate differences between dates in various units
- 🌐 Relative Time — Human-readable relative time ("3 hours ago", "2 days later") with i18n
- 📅 Parse — Parse date strings and construct dates from parts
- 📆 Misc — Weekend checks, weekday names, leap year, day of year, timezone helpers
Installation
npm install xkernel
# or
pnpm add xkernel
# or
yarn add xkernelUsage
import { formatDate, add, diff, fromNow, isWeekend, daysInMonth } from 'xkernel';
// Formatting
formatDate(new Date(2024, 0, 15), 'YYYY-MM-DD'); // '2024-01-15'
formatDate(new Date(2024, 0, 15, 9, 5), 'YYYY/MM/DD HH:mm'); // '2024/01/15 09:05'
// Calculation
add(new Date(2024, 0, 15), 1, 'day'); // 2024-01-16
add(new Date(2024, 0, 15), -1, 'month'); // 2023-12-15
// Diff
diff(new Date(2024, 0, 1), new Date(2024, 0, 10), 'day'); // 9
// Relative time
setRelativeTimeLocale('zh');
fromNow(new Date(2024, 0, 15), new Date(2024, 0, 14)); // '1天后'
// Misc
isWeekend(new Date(2024, 0, 6)); // true (Saturday)
daysInMonth(2024, 2); // 29 (leap year)
isLeapYear(2024); // trueAPI Reference
Format
formatDate(date, format?)— Format a date to string with tokens (YYYY,MM,DD,HH,mm,ss,SSS, etc.)toISOString(date)— Convert to ISO 8601 string
Calculation
add(date, amount, unit)— Add/subtract time by unitaddDays(date, amount)/addMonths(date, amount)/addYears(date, amount)addHours(date, amount)/addMinutes(date, amount)/addSeconds(date, amount)
Comparison
isValidDate(value)— Check if value is a valid DateisDateLike(value)— Check if value can be parsed as datecompareDates(a, b)— Compare two dates (-1, 0, 1)isBefore(a, b)/isAfter(a, b)— Relative comparisonisSameDay(a, b)/isSameMonth(a, b)/isSameYear(a, b)isToday(date)/isYesterday(date)/isTomorrow(date)isThisWeek(date, refDate?)
Diff
diff(dateA, dateB, unit)— Difference in specified unitdiffInDays(a, b)/diffInHours(a, b)/diffInMinutes(a, b)/diffInSeconds(a, b)startOfDay(date)/endOfDay(date)startOfWeek(date)/endOfWeek(date)startOfMonth(date)/endOfMonth(date)startOfYear(date)/endOfYear(date)
Relative Time
fromNow(date, from?)— Human-readable relative timehumanizeDuration(ms)— Convert milliseconds to readable durationsetRelativeTimeLocale(locale)— Set locale ('en' | 'zh')
Parse
parseDate(value)— Parse string/number to DateparseToTimestamp(value)— Parse to timestampsafeParseDate(value)— Parse, return null on failurefromDateParts(parts)— Construct date from components
Misc
isWeekend(date)/isWeekday(date)countWeekdays(start, end)— Count weekdays in rangegetTimezoneOffset(date?)— Get timezone offsetconvertToTimezone(date, offsetMinutes)— Convert to target timezonegetWeekdayName(date, locale?)— Get weekday namegetISODay(date)— ISO weekday number (1=Mon, 7=Sun)daysInMonth(year, month)— Days in given monthisLeapYear(year)— Check leap yeardayOfYear(date)— Day number in year (1-366)now()— Current timestamp
License
MIT
