All files / src/utils date.ts

22.81% Statements 13/57
8.57% Branches 3/35
16.67% Functions 2/12
23.08% Lines 12/52

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 1491x         1x 1x 4x       4x     1x         1x                                             1x                                               1x                                       1x 3x     1x                                                                                                                    
import moment from 'moment';
 
/**
 * Date utils.
 */
export class DateUtils {
  public static convertTimeZone = (date: Date, timezone?: string) => {
    Iif (timezone && date) {
      return moment(date).tz(timezone).format();
    }
 
    return date;
  };
 
  public static formatDateAndConvertTimeZone = (dateString: Date | string, format = 'YYYYMMDD', timezone: string) => {
    const date = moment.tz(dateString, timezone);
    return date.format(format);
  }
 
  public static dateFormatForXMLFile = (date: Date | string, tz?: string) => {
    if (!date) {
      return '';
    }
  
    if (!tz) {
      tz = 'UTC';
    }
 
    if (typeof date === 'string') { date = new Date(date) };
    return new Intl.DateTimeFormat('en-GB', {
      timeZoneName: 'short',
      year: 'numeric',
      month: 'numeric',
      day: 'numeric',
      hour: 'numeric',
      minute: 'numeric',
      second: 'numeric',
      timeZone: tz,
    }).format(new Date(date));
  };
  
 
  public static formatDateWithTimeZone = (date: Date | string, timezone?: string) => {
    if (!timezone) {
      timezone = 'UTC';
    }
 
    if (typeof date === 'string') { date = new Date(date) };
    return new Intl.DateTimeFormat('default', {
      weekday: 'long',
      year: 'numeric',
      month: 'long',
      day: 'numeric',
      hour: 'numeric',
      minute: 'numeric',
      second: 'numeric',
      timeZone: timezone,
      timeZoneName: 'short',
    }).format(date);
  };
 
  /**
   * Format date
   * @param {timestamptz} date Date need to format
   * @return {string} Return date with format: dd/MM/yy HH:mm.
   */
  public static formatDateWithTimeZoneForPDF = (date: Date | string, timezone?: string): string => {
    if (!date) {
      return '';
    }
 
    if (!timezone) {
      timezone = 'UTC';
    }
 
    return new Intl.DateTimeFormat('en-GB', {
      timeZoneName: 'short',
      year: 'numeric',
      month: 'numeric',
      day: 'numeric',
      hour: 'numeric',
      minute: 'numeric',
      timeZone: timezone,
    }).format(new Date(date));
  };
 
  public static formatDate = (date?: Date | string, format = 'YYYYMMDD') => {
    Eif (date) { return moment(date).format(format) }
  };
 
  public static format = (date?: Date | string, format = 'YYYYMMDD') => {
    if (date) { return +moment(date).format('YYYYMMDD') }
  };
 
  public static isIsoDate(str: string) {
    if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false;
    try {
      const d = new Date(str);
      return d.toISOString() === str;
    } catch(error) {
      return false;
    }
  }
 
  public static getDateAfterDays(days: number) {
    const now = moment().utcOffset(0);
    now.set({hour: 0, minute: 0, second: 0, millisecond: 0});
 
    return now.subtract(days, 'days').toISOString();
  }
 
  public static isCurrentOrFutureDate(date?: Date | string) {
    if (date && this.isIsoDate(date.toString())) {
      const dateObj = moment(date);
 
      return dateObj.isSameOrAfter(moment());
    }
 
    return false;
  }
  
  public static isSameOrAfter(date1: string, date2: string) {
    const objDate1 = moment(date1);
    const objDate2 = moment(date2);
 
    return objDate1.isSameOrAfter(objDate2);
  }
 
  /**
   * Adjusts the given date by adding the specified number of days.
   *
   * @param {Date} date - the date to be adjusted
   * @param {number} adjustment - the number of days to add
   * @return {Date} the adjusted date
   */
  public static adjustDays(date: Date, adjustment: number): Date {
    let newDate = moment(date);
    if (adjustment !== 0) {
      if (adjustment > 0) {
        newDate = newDate.add(Math.abs(adjustment), 'd');
      } else {
        newDate = newDate.subtract(Math.abs(adjustment), 'd');
      }
    }
 
    return newDate.toDate();
  }
}