PluginProbe
Parse.ly / 3.8.4
Parse.ly v3.8.4
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.8.4, at src/blocks/shared/utils/date.ts

94 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * External dependencies
3 */
4 import { __ } from '@wordpress/i18n';
5
6 const SHORT_DATE_FORMAT: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' };
7 const SHORT_DATE_FORMAT_WITHOUT_YEAR: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric' };
8 const DATE_NOT_AVAILABLE_MESSAGE = __( 'Date N/A', 'wp-parsely' );
9
10 /**
11 * Returns whether the passed date can be processed further.
12 *
13 * @param {Date} date The date to be examined.
14 * @return {boolean} Whether the date can be processed further.
15 */
16 export function canProcessDate( date: Date|string ): boolean {
17 if ( 'string' === typeof date ) {
18 date = new Date( date );
19 }
20
21 const isValidDateObject = date instanceof Date && ! isNaN( +date );
22 const isNotEpochDate = 0 !== date.getTime();
23
24 return isValidDateObject && isNotEpochDate;
25 }
26
27 export function getDateInUserLang( date: Date, options: Intl.DateTimeFormatOptions ): string {
28 if ( false === canProcessDate( date ) ) {
29 return DATE_NOT_AVAILABLE_MESSAGE;
30 }
31
32 return Intl.DateTimeFormat(
33 document.documentElement.lang || 'en',
34 options
35 ).format( date );
36 }
37
38 /**
39 * Returns the passed date in short format or in short format without year (if
40 * the passed date is within the current year), respecting the user's language.
41 *
42 * @param {Date} date The date to be formatted.
43 * @return {string} The resulting date in its final format.
44 */
45 export function getSmartShortDate( date: Date ): string {
46 if ( false === canProcessDate( date ) ) {
47 return DATE_NOT_AVAILABLE_MESSAGE;
48 }
49
50 let dateFormat = SHORT_DATE_FORMAT;
51
52 if ( date.getUTCFullYear() === new Date().getUTCFullYear() ) {
53 dateFormat = SHORT_DATE_FORMAT_WITHOUT_YEAR;
54 }
55
56 return Intl.DateTimeFormat(
57 document.documentElement.lang || 'en',
58 dateFormat
59 ).format( date );
60 }
61
62 /**
63 * Removes the given number of days from a "YYYY-MM-DD" string, and returns
64 * the result in the same format.
65 *
66 * @param {string} date The date in "YYYY-MM-DD" format.
67 * @param {number} days The number of days to remove from the date.
68 * @return {string} The resulting date in "YYYY-MM-DD" format.
69 */
70 export function removeDaysFromDate( date: string, days: number ): string {
71 if ( false === canProcessDate( date ) ) {
72 return DATE_NOT_AVAILABLE_MESSAGE;
73 }
74
75 const pastDate = new Date( date );
76 pastDate.setDate( pastDate.getDate() - days );
77
78 return convertDateToString( pastDate );
79 }
80
81 /**
82 * Converts a date to a string in "YYYY-MM-DD" format.
83 *
84 * @param {Date} date The date to format.
85 * @return {string} The date in "YYYY-MM-DD" format.
86 */
87 export function convertDateToString( date: Date ): string {
88 if ( false === canProcessDate( date ) ) {
89 return DATE_NOT_AVAILABLE_MESSAGE;
90 }
91
92 return date.toISOString().substring( 0, 10 );
93 }
94