UNPKG

ts-time-utils

Version:

A comprehensive TypeScript utility library for time, dates, durations, and calendar operations with full tree-shaking support

109 lines (108 loc) 3.08 kB
/** * Check if a date is valid * @param date - date to validate */ export function isValidDate(date) { if (date instanceof Date) { return !isNaN(date.getTime()); } if (typeof date === 'string' || typeof date === 'number') { const parsed = new Date(date); return !isNaN(parsed.getTime()); } return false; } /** * Check if a year is a leap year * @param year - year to check */ export function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0); } /** * Check if a date is in the past * @param date - date to check */ export function isPast(date) { return date.getTime() < new Date().getTime(); } /** * Check if a date is in the future * @param date - date to check */ export function isFuture(date) { return date.getTime() > new Date().getTime(); } /** * Check if a date is today * @param date - date to check */ export function isToday(date) { const today = new Date(); return (date.getDate() === today.getDate() && date.getMonth() === today.getMonth() && date.getFullYear() === today.getFullYear()); } /** * Check if a date is yesterday * @param date - date to check */ export function isYesterday(date) { const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); return (date.getDate() === yesterday.getDate() && date.getMonth() === yesterday.getMonth() && date.getFullYear() === yesterday.getFullYear()); } /** * Check if a date is tomorrow * @param date - date to check */ export function isTomorrow(date) { const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); return (date.getDate() === tomorrow.getDate() && date.getMonth() === tomorrow.getMonth() && date.getFullYear() === tomorrow.getFullYear()); } /** * Check if two dates are the same day * @param date1 - first date * @param date2 - second date */ export function isSameDay(date1, date2) { return (date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear()); } /** * Check if a date is a weekend (Saturday or Sunday) * @param date - date to check */ export function isWeekend(date) { const dayOfWeek = date.getDay(); return dayOfWeek === 0 || dayOfWeek === 6; // Sunday or Saturday } /** * Check if a date is a weekday (Monday through Friday) * @param date - date to check */ export function isWeekday(date) { return !isWeekend(date); } /** * Check if a time string is valid (HH:MM or HH:MM:SS format) * @param time - time string to validate */ export function isValidTimeString(time) { const timeRegex = /^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/; return timeRegex.test(time); } /** * Check if a date string is valid ISO 8601 format * @param dateString - date string to validate */ export function isValidISOString(dateString) { const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/; return isoRegex.test(dateString) && isValidDate(dateString); }