UNPKG

atikin-timebuddy

Version:

Atikin TimeBuddy – Lightweight Date & Time Utilities for Everyday Use

82 lines (68 loc) 2.14 kB
/** * Atikin TimeBuddy – Lightweight Date & Time Utilities * Author: Atikin Verse */ function timeAgo(dateString) { const now = new Date(); const then = new Date(dateString); const diff = Math.floor((now - then) / 1000); if (diff < 60) return `${diff} seconds ago`; if (diff < 3600) return `${Math.floor(diff / 60)} minutes ago`; if (diff < 86400) return `${Math.floor(diff / 3600)} hours ago`; if (diff < 604800) return `${Math.floor(diff / 86400)} days ago`; return then.toDateString(); } function formatDate(date = new Date(), format = 'short') { const options = format === 'short' ? { day: '2-digit', month: 'short', year: 'numeric' } : { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; return new Intl.DateTimeFormat('en-US', options).format(date); } function getTimeInZone(timeZone = 'UTC') { return new Date().toLocaleString('en-US', { timeZone }); } function addDays(date = new Date(), days = 1) { const result = new Date(date); result.setDate(result.getDate() + days); return result; } function subtractHours(date = new Date(), hours = 1) { const result = new Date(date); result.setHours(result.getHours() - hours); return result; } function isWeekend(date = new Date()) { const day = new Date(date).getDay(); return day === 0 || day === 6; } function workingDaysBetween(start, end) { let count = 0; const current = new Date(start); const target = new Date(end); while (current <= target) { const day = current.getDay(); if (day !== 0 && day !== 6) count++; current.setDate(current.getDate() + 1); } return count; } function getRelativeDay(inputDate) { const today = new Date(); const date = new Date(inputDate); const diff = Math.floor((date - today) / (1000 * 60 * 60 * 24)); if (diff === 0) return 'Today'; if (diff === -1) return 'Yesterday'; if (diff === 1) return 'Tomorrow'; if (diff < 0) return `${Math.abs(diff)} days ago`; return `In ${diff} days`; } module.exports = { timeAgo, formatDate, getTimeInZone, addDays, subtractHours, isWeekend, workingDaysBetween, getRelativeDay, };