dev-utils-plus
Version:
Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math
168 lines • 4.89 kB
JavaScript
;
/**
* Date utility functions for common operations
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatDate = formatDate;
exports.startOfDay = startOfDay;
exports.endOfDay = endOfDay;
exports.addDays = addDays;
exports.subtractDays = subtractDays;
exports.daysDifference = daysDifference;
exports.isToday = isToday;
exports.isPast = isPast;
exports.isFuture = isFuture;
exports.getAge = getAge;
exports.getWeekNumber = getWeekNumber;
exports.isLeapYear = isLeapYear;
exports.getDaysInMonth = getDaysInMonth;
exports.parseDate = parseDate;
exports.getRelativeTime = getRelativeTime;
/**
* Formats a date to a readable string
*/
function formatDate(date, format = 'YYYY-MM-DD') {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return format
.replace('YYYY', String(year))
.replace('MM', month)
.replace('DD', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds);
}
/**
* Gets the start of a day (00:00:00)
*/
function startOfDay(date) {
const newDate = new Date(date);
newDate.setHours(0, 0, 0, 0);
return newDate;
}
/**
* Gets the end of a day (23:59:59.999)
*/
function endOfDay(date) {
const newDate = new Date(date);
newDate.setHours(23, 59, 59, 999);
return newDate;
}
/**
* Adds days to a date
*/
function addDays(date, days) {
const newDate = new Date(date);
newDate.setDate(date.getDate() + days);
return newDate;
}
/**
* Subtracts days from a date
*/
function subtractDays(date, days) {
return addDays(date, -days);
}
/**
* Gets the difference between two dates in days
*/
function daysDifference(date1, date2) {
const timeDiff = Math.abs(date2.getTime() - date1.getTime());
return Math.ceil(timeDiff / (1000 * 3600 * 24));
}
/**
* Checks if a date is today
*/
function isToday(date) {
const today = new Date();
return (date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
date.getFullYear() === today.getFullYear());
}
/**
* Checks if a date is in the past
*/
function isPast(date) {
return date < new Date();
}
/**
* Checks if a date is in the future
*/
function isFuture(date) {
return date > new Date();
}
/**
* Gets the age from a birth date
*/
function getAge(birthDate) {
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
/**
* Gets the week number of a date
*/
function getWeekNumber(date) {
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
const pastDaysOfYear = (date.getTime() - firstDayOfYear.getTime()) / 86400000;
return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}
/**
* Checks if a year is a leap year
*/
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
/**
* Gets the number of days in a month
*/
function getDaysInMonth(year, month) {
return new Date(year, month + 1, 0).getDate();
}
/**
* Parses a date string to a Date object
*/
function parseDate(dateString) {
const date = new Date(dateString);
return isNaN(date.getTime()) ? null : date;
}
/**
* Gets a relative time string (e.g., "2 hours ago")
*/
function getRelativeTime(date) {
const now = new Date();
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
if (diffInSeconds < 60) {
return 'just now';
}
const diffInMinutes = Math.floor(diffInSeconds / 60);
if (diffInMinutes < 60) {
return `${diffInMinutes} minute${diffInMinutes === 1 ? '' : 's'} ago`;
}
const diffInHours = Math.floor(diffInMinutes / 60);
if (diffInHours < 24) {
return `${diffInHours} hour${diffInHours === 1 ? '' : 's'} ago`;
}
const diffInDays = Math.floor(diffInHours / 24);
if (diffInDays < 7) {
return `${diffInDays} day${diffInDays === 1 ? '' : 's'} ago`;
}
const diffInWeeks = Math.floor(diffInDays / 7);
if (diffInWeeks < 4) {
return `${diffInWeeks} week${diffInWeeks === 1 ? '' : 's'} ago`;
}
const diffInMonths = Math.floor(diffInDays / 30);
if (diffInMonths < 12) {
return `${diffInMonths} month${diffInMonths === 1 ? '' : 's'} ago`;
}
const diffInYears = Math.floor(diffInDays / 365);
return `${diffInYears} year${diffInYears === 1 ? '' : 's'} ago`;
}
//# sourceMappingURL=index.js.map