PluginProbe
Parse.ly / 3.7.1
Parse.ly v3.7.1
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / blocks / shared / utils / date.ts

date.ts in Parse.ly 3.7.1, at src/blocks/shared/utils/date.ts

55 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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