| 1 |
export const SHORT_DATE_FORMAT: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' }; |
| 2 |
export const SHORT_DATE_FORMAT_WITHOUT_YEAR: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric' }; |
| 3 |
|
| 4 |
export function getDateInUserLang( date: Date, options: Intl.DateTimeFormatOptions ): string { |
| 5 |
return Intl.DateTimeFormat( |
| 6 |
document.documentElement.lang || 'en', |
| 7 |
options |
| 8 |
).format( date ); |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Returns the passed date in short format or in short format without year (if |
| 13 |
* the passed date is within the current year), respecting the user's language. |
| 14 |
* |
| 15 |
* @param {Date} date The date to be formatted. |
| 16 |
* @return {string} The resulting date in its final format. |
| 17 |
*/ |
| 18 |
export function getSmartShortDate( date: Date ): string { |
| 19 |
let dateFormat = SHORT_DATE_FORMAT; |
| 20 |
|
| 21 |
if ( date.getUTCFullYear() === new Date().getUTCFullYear() ) { |
| 22 |
dateFormat = SHORT_DATE_FORMAT_WITHOUT_YEAR; |
| 23 |
} |
| 24 |
|
| 25 |
return Intl.DateTimeFormat( |
| 26 |
document.documentElement.lang || 'en', |
| 27 |
dateFormat |
| 28 |
).format( date ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Removes the given number of days from a "YYYY-MM-DD" string, and returns |
| 33 |
* the result in the same format. |
| 34 |
* |
| 35 |
* @param {string} date The date in "YYYY-MM-DD" format. |
| 36 |
* @param {number} days The number of days to remove from the date. |
| 37 |
* @return {string} The resulting date in "YYYY-MM-DD" format. |
| 38 |
*/ |
| 39 |
export function removeDaysFromDate( date: string, days: number ): string { |
| 40 |
const pastDate = new Date( date ); |
| 41 |
pastDate.setDate( pastDate.getDate() - days ); |
| 42 |
|
| 43 |
return convertDateToString( pastDate ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Converts a date to a string in "YYYY-MM-DD" format. |
| 48 |
* |
| 49 |
* @param {Date} date The date to format. |
| 50 |
* @return {string} The date in "YYYY-MM-DD" format. |
| 51 |
*/ |
| 52 |
export function convertDateToString( date: Date ): string { |
| 53 |
return date.toISOString().substring( 0, 10 ); |
| 54 |
} |
| 55 |
|