workspace/core/modules/utils/date.util.ts
rckrdmrd 49155822ae fix: Resolve TypeScript compilation errors across all projects
Platform Marketing Content:
- Add PaginationParams, PaginationMeta, PaginatedResponse interfaces
- Fix JwtAuthGuard import paths (common/guards instead of modules/auth)
- Add missing fields to CRM interfaces (address, keywords, features, benefits)
- Install @nestjs/throttler dependency

ERP Suite - Construccion:
- Create tsconfig.node.json for web frontend
- Add vite-env.d.ts for Vite types
- Fix implicit return errors in Express controllers
- Prefix unused parameters with underscore

ERP Suite - ERP Core:
- Export PoolClient type from database config
- Fix invoice type comparison (customer/supplier vs out_invoice)
- Refactor base.service.ts query handling for proper type inference
- Rename Role type to RoleType to avoid conflict with entity
- Fix ProtectedRoute to use role?.name instead of roles array

ERP Suite - POS Micro:
- Add vite-env.d.ts for Vite types
- Fix Sale property names (discountAmount, changeAmount)
- Export TodaySummary interface from sales service

All projects now pass npm install and npm run build successfully.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-08 22:35:55 -06:00

249 lines
5.8 KiB
TypeScript

/**
* Date Utilities - Core Module
*
* Framework-agnostic date manipulation and formatting functions.
* Can be used in any project (NestJS, Express, Frontend, etc.)
*
* @module @core/utils/date
* @version 1.0.0
*/
/**
* Format date to ISO string
*/
export const formatToISO = (date: Date): string => {
return date.toISOString();
};
/**
* Format date to readable string (YYYY-MM-DD)
*/
export const formatToDate = (date: Date): string => {
return date.toISOString().split('T')[0];
};
/**
* Format date to datetime string (YYYY-MM-DD HH:mm:ss)
*/
export const formatToDateTime = (date: Date): string => {
return date.toISOString().replace('T', ' ').split('.')[0];
};
/**
* Format date with custom pattern
* @param date - Date to format
* @param pattern - Pattern (YYYY, MM, DD, HH, mm, ss)
*/
export const formatDate = (date: Date, pattern: string): string => {
const pad = (n: number): string => n.toString().padStart(2, '0');
return pattern
.replace('YYYY', date.getFullYear().toString())
.replace('MM', pad(date.getMonth() + 1))
.replace('DD', pad(date.getDate()))
.replace('HH', pad(date.getHours()))
.replace('mm', pad(date.getMinutes()))
.replace('ss', pad(date.getSeconds()));
};
/**
* Add days to a date
*/
export const addDays = (date: Date, days: number): Date => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
};
/**
* Add hours to a date
*/
export const addHours = (date: Date, hours: number): Date => {
const result = new Date(date);
result.setHours(result.getHours() + hours);
return result;
};
/**
* Add minutes to a date
*/
export const addMinutes = (date: Date, minutes: number): Date => {
const result = new Date(date);
result.setMinutes(result.getMinutes() + minutes);
return result;
};
/**
* Add months to a date
*/
export const addMonths = (date: Date, months: number): Date => {
const result = new Date(date);
result.setMonth(result.getMonth() + months);
return result;
};
/**
* Check if date is in the past
*/
export const isPast = (date: Date): boolean => {
return date < new Date();
};
/**
* Check if date is in the future
*/
export const isFuture = (date: Date): boolean => {
return date > new Date();
};
/**
* Get start of day (00:00:00.000)
*/
export const startOfDay = (date: Date): Date => {
const result = new Date(date);
result.setHours(0, 0, 0, 0);
return result;
};
/**
* Get end of day (23:59:59.999)
*/
export const endOfDay = (date: Date): Date => {
const result = new Date(date);
result.setHours(23, 59, 59, 999);
return result;
};
/**
* Get start of month
*/
export const startOfMonth = (date: Date): Date => {
const result = new Date(date);
result.setDate(1);
result.setHours(0, 0, 0, 0);
return result;
};
/**
* Get end of month
*/
export const endOfMonth = (date: Date): Date => {
const result = new Date(date);
result.setMonth(result.getMonth() + 1, 0);
result.setHours(23, 59, 59, 999);
return result;
};
/**
* Get difference in days between two dates
*/
export const diffInDays = (date1: Date, date2: Date): number => {
const diff = Math.abs(date1.getTime() - date2.getTime());
return Math.ceil(diff / (1000 * 60 * 60 * 24));
};
/**
* Get difference in hours between two dates
*/
export const diffInHours = (date1: Date, date2: Date): number => {
const diff = Math.abs(date1.getTime() - date2.getTime());
return Math.ceil(diff / (1000 * 60 * 60));
};
/**
* Get difference in minutes between two dates
*/
export const diffInMinutes = (date1: Date, date2: Date): number => {
const diff = Math.abs(date1.getTime() - date2.getTime());
return Math.ceil(diff / (1000 * 60));
};
/**
* Check if two dates are the same day
*/
export const isSameDay = (date1: Date, date2: Date): boolean => {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
);
};
/**
* Parse ISO string to Date
*/
export const parseISO = (isoString: string): Date => {
return new Date(isoString);
};
/**
* Check if string is valid date
*/
export const isValidDate = (dateString: string): boolean => {
const date = new Date(dateString);
return !isNaN(date.getTime());
};
/**
* Get relative time description (e.g., "2 hours ago")
*/
export const getRelativeTime = (date: Date): string => {
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHour = Math.floor(diffMin / 60);
const diffDay = Math.floor(diffHour / 24);
if (diffSec < 60) return 'just now';
if (diffMin < 60) return `${diffMin} minute${diffMin > 1 ? 's' : ''} ago`;
if (diffHour < 24) return `${diffHour} hour${diffHour > 1 ? 's' : ''} ago`;
if (diffDay < 30) return `${diffDay} day${diffDay > 1 ? 's' : ''} ago`;
return formatToDate(date);
};
/**
* Get Unix timestamp (seconds since epoch)
*/
export const toUnixTimestamp = (date: Date): number => {
return Math.floor(date.getTime() / 1000);
};
/**
* Create date from Unix timestamp
*/
export const fromUnixTimestamp = (timestamp: number): Date => {
return new Date(timestamp * 1000);
};
/**
* Check if date is today
*/
export const isToday = (date: Date): boolean => {
return isSameDay(date, new Date());
};
/**
* Check if date is yesterday
*/
export const isYesterday = (date: Date): boolean => {
const yesterday = addDays(new Date(), -1);
return isSameDay(date, yesterday);
};
/**
* Get age from birthdate
*/
export const getAge = (birthDate: Date): number => {
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;
};