All files / src/lib date-utils.ts

88.09% Statements 37/42
100% Branches 28/28
100% Functions 5/5
88.09% Lines 37/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104              5x 19x 19x   13x   10x 10x     3x   6x 3x   3x                       5x 19x 19x 13x   9x 9x         9x     4x   6x 3x   3x                       5x 22x 22x 15x 6x 9x 8x 8x         8x   7x 4x   4x                   5x 4x             5x 4x    
import { format, parseISO, isValid } from 'date-fns';
 
/**
 * Formats a date string or Date object to DD/MM/YYYY format
 * @param date - Date string (ISO format) or Date object
 * @returns Formatted date string in DD/MM/YYYY format
 */
export const formatDate = (date: string | Date): string => {
  try {
    if (typeof date === 'string') {
      // Handle both ISO dates (from API) and DD/MM/YYYY format
      if (date.includes('-')) {
        // ISO format: 2024-01-14
        const parsedDate = parseISO(date);
        return isValid(parsedDate) ? format(parsedDate, 'dd/MM/yyyy') : date;
      } else {
        // Already in DD/MM/YYYY format
        return date;
      }
    } else if (date instanceof Date) {
      return isValid(date) ? format(date, 'dd/MM/yyyy') : 'Invalid Date';
    }
    return 'Invalid Date';
  } catch (error) {
    console.error('Error formatting date:', error);
    return 'Invalid Date';
  }
};
 
/**
 * Formats a date for API submission (ISO format)
 * @param date - Date string or Date object
 * @returns ISO formatted date string (YYYY-MM-DD)
 */
export const formatDateForAPI = (date: string | Date): string => {
  try {
    if (typeof date === 'string') {
      if (date.includes('/')) {
        // Convert DD/MM/YYYY to ISO format
        const [day, month, year] = date.split('/');
        const dateObj = new Date(
          parseInt(year),
          parseInt(month) - 1,
          parseInt(day)
        );
        return isValid(dateObj) ? format(dateObj, 'yyyy-MM-dd') : date;
      } else {
        // Already in ISO format
        return date;
      }
    } else if (date instanceof Date) {
      return isValid(date) ? format(date, 'yyyy-MM-dd') : '';
    }
    return '';
  } catch (error) {
    console.error('Error formatting date for API:', error);
    return '';
  }
};
 
/**
 * Checks if a date string is in valid format
 * @param date - Date string to validate
 * @returns boolean indicating if date is valid
 */
export const isValidDate = (date: string | Date): boolean => {
  try {
    if (typeof date === 'string') {
      if (date.includes('-')) {
        return isValid(parseISO(date));
      } else if (date.includes('/')) {
        const [day, month, year] = date.split('/');
        const dateObj = new Date(
          parseInt(year),
          parseInt(month) - 1,
          parseInt(day)
        );
        return isValid(dateObj);
      }
    } else if (date instanceof Date) {
      return isValid(date);
    }
    return false;
  } catch (error) {
    return false;
  }
};
 
/**
 * Gets the current date in DD/MM/YYYY format
 * @returns Current date formatted as DD/MM/YYYY
 */
export const getCurrentDate = (): string => {
  return format(new Date(), 'dd/MM/yyyy');
};
 
/**
 * Gets the current date in ISO format for API
 * @returns Current date formatted as YYYY-MM-DD
 */
export const getCurrentDateISO = (): string => {
  return format(new Date(), 'yyyy-MM-dd');
};