PluginProbe
Parse.ly / 3.20.6
Parse.ly v3.20.6
3.24.2 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 All 106 releases
wp-parsely / src / content-helper / common / utils / date.ts

date.ts in Parse.ly 3.20.6, at src/content-helper/common/utils/date.ts

97 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WordPress 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 ): boolean {
17 // Return false if the date is not a valid Date object.
18 if ( isNaN( +date ) ) {
19 return false;
20 }
21
22 // Return false if the date is the Unix Epoch time.
23 return 0 !== date.getTime();
24 }
25
26 export function getDateInUserLang( date: Date, options: Intl.DateTimeFormatOptions ): string {
27 if ( false === canProcessDate( date ) ) {
28 return DATE_NOT_AVAILABLE_MESSAGE;
29 }
30
31 return Intl.DateTimeFormat(
32 document.documentElement.lang || 'en',
33 options
34 ).format( date );
35 }
36
37 /**
38 * Returns the passed date in short format or in short format without year (if
39 * the passed date is within the current year), respecting the user's language.
40 *
41 * @param {Date} date The date to be formatted.
42 * @return {string} The resulting date in its final format.
43 */
44 export function getSmartShortDate( date: Date ): string {
45 if ( false === canProcessDate( date ) ) {
46 return DATE_NOT_AVAILABLE_MESSAGE;
47 }
48
49 let dateFormat = SHORT_DATE_FORMAT;
50
51 if ( date.getUTCFullYear() === new Date().getUTCFullYear() ) {
52 dateFormat = SHORT_DATE_FORMAT_WITHOUT_YEAR;
53 }
54
55 return Intl.DateTimeFormat(
56 document.documentElement.lang || 'en',
57 dateFormat
58 ).format( date );
59 }
60
61 /**
62 * Removes the given number of days from a "YYYY-MM-DD" string, and returns
63 * the result in the same format.
64 *
65 * @param {Date} date The date to be processed.
66 * @param {number} days The number of days to remove from the date.
67 * @return {string} The resulting date in "YYYY-MM-DD" format.
68 */
69 export function removeDaysFromDate( date: Date, days: number ): string {
70 if ( false === Number.isInteger( days ) ) {
71 return __( 'days parameter must be an integer', 'wp-parsely' );
72 }
73
74 if ( false === canProcessDate( date ) ) {
75 return DATE_NOT_AVAILABLE_MESSAGE;
76 }
77
78 const pastDate = new Date( date );
79 pastDate.setDate( pastDate.getDate() - days );
80
81 return convertDateToString( pastDate );
82 }
83
84 /**
85 * Converts a date to a string in "YYYY-MM-DD" format.
86 *
87 * @param {Date} date The date to format.
88 * @return {string} The date in "YYYY-MM-DD" format.
89 */
90 export function convertDateToString( date: Date ): string {
91 if ( false === canProcessDate( date ) ) {
92 return DATE_NOT_AVAILABLE_MESSAGE;
93 }
94
95 return date.toISOString().substring( 0, 10 );
96 }
97