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 | 17x 17x 17x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 2x 1x 13x 11x 11x 12x 5x 2x 2x | import { Config, I18n, dayjs } from '..';
import { IDateHelperValues } from './interfaces';
export class DateHelper {
/**
* Helper default values
*/
private static values: IDateHelperValues = {
TODAY: {
label: 'DATE_HELPERVALUE_TODAY',
fn: () => dayjs().toDate(),
},
TOMORROW: {
label: 'DATE_HELPERVALUE_TOMORROW',
fn: () => dayjs().add(1, 'day').toDate(),
},
YESTERDAY: {
label: 'DATE_HELPERVALUE_YESTERDAY',
fn: () => dayjs().subtract(1, 'day').toDate(),
},
LAST_7_DAYS: {
label: 'DATE_HELPERVALUE_LAST_7_DAYS',
fn: () => {
const end = dayjs().toDate();
const start = dayjs().subtract(7, 'day').toDate();
return [start, end];
},
},
NEXT_7_DAYS: {
label: 'DATE_HELPERVALUE_NEXT_7_DAYS',
fn: () => {
const start = dayjs().toDate();
const end = dayjs().add(7, 'day').toDate();
return [start, end];
},
},
CURRENT_WEEK: {
label: 'DATE_HELPERVALUE_CURRENT_WEEK',
fn: () => {
const start = dayjs().startOf('week').toDate();
const end = dayjs().endOf('week').toDate();
return [start, end];
},
},
CURRENT_MONTH: {
label: 'DATE_HELPERVALUE_CURRENT_MONTH',
fn: () => {
const start = dayjs().startOf('month').toDate();
const end = dayjs().endOf('month').toDate();
return [start, end];
},
},
CURRENT_YEAR: {
label: 'DATE_HELPERVALUE_CURRENT_YEAR',
fn: () => {
const start = dayjs().startOf('year').toDate();
const end = dayjs().endOf('year').toDate();
return [start, end];
},
},
};
/**
* Format a date from a specified format
* @param date
* @param format
* @returns
*/
private static formatDate(date: Date, format?: string) {
return dayjs(date).format(format || Config.dateFormat);
}
/**
* Returns the translated label of the helper function
* @param name
* @returns
*/
public static getLabel(name: string) {
if (!DateHelper.values[name]) return '';
return I18n.translate(DateHelper.values[name].label);
}
/**
* Returns the value calculated by the auxiliary function considering the current date.
* The return can be a single value or a list of values.
* If the parameter that defines the date format is not informed,
* the value configured by the Config class will be used.
* @param name
* @param format
* @returns
*/
public static getValue(name: string, format?: string) {
if (!DateHelper.values[name]) return '';
const value = DateHelper.values[name].fn();
if (Array.isArray(value)) {
return value.map((item) => DateHelper.formatDate(item, format));
}
return DateHelper.formatDate(value, format);
}
/**
* Registers a new function in the class passing a name, a label (which must be
* translated) and afunction that returns a single value or an array of values.
* @param name
* @param label
* @param fn
*/
public static register(name: string, label: string, fn: () => Date | [Date, Date]) {
DateHelper.values[name] = {
label,
fn,
};
}
/**
* Removes a function from the registry.
* @param name
*/
public static unregister(name: string) {
delete DateHelper.values[name];
}
}
|