# wp-parsely/3.20.6/src/content-helper/common/utils/date.ts

Parse.ly, version 3.20.6. 97 lines.

- Page: https://pluginprobe.com/plugins/wp-parsely/3.20.6/code/src/content-helper/common/utils/date.ts
- Raw: https://pluginprobe.com/plugins/wp-parsely/3.20.6/raw/src/content-helper/common/utils/date.ts
- Modified: 2023-09-04T11:12:04+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wp-parsely/3.20.6/code/src/content-helper/common/utils/date.ts#L10-L20`.

```typescript
/**
 * WordPress dependencies
 */
import { __ } from '@wordpress/i18n';

const SHORT_DATE_FORMAT: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' };
const SHORT_DATE_FORMAT_WITHOUT_YEAR: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric' };
const DATE_NOT_AVAILABLE_MESSAGE = __( 'Date N/A', 'wp-parsely' );

/**
 * Returns whether the passed date can be processed further.
 *
 * @param {Date} date The date to be examined.
 * @return {boolean} Whether the date can be processed further.
 */
export function canProcessDate( date: Date ): boolean {
	// Return false if the date is not a valid Date object.
	if ( isNaN( +date ) ) {
		return false;
	}

	// Return false if the date is the Unix Epoch time.
	return 0 !== date.getTime();
}

export function getDateInUserLang( date: Date, options: Intl.DateTimeFormatOptions ): string {
	if ( false === canProcessDate( date ) ) {
		return DATE_NOT_AVAILABLE_MESSAGE;
	}

	return Intl.DateTimeFormat(
		document.documentElement.lang || 'en',
		options
	).format( date );
}

/**
 * Returns the passed date in short format or in short format without year (if
 * the passed date is within the current year), respecting the user's language.
 *
 * @param {Date} date The date to be formatted.
 * @return {string} The resulting date in its final format.
 */
export function getSmartShortDate( date: Date ): string {
	if ( false === canProcessDate( date ) ) {
		return DATE_NOT_AVAILABLE_MESSAGE;
	}

	let dateFormat = SHORT_DATE_FORMAT;

	if ( date.getUTCFullYear() === new Date().getUTCFullYear() ) {
		dateFormat = SHORT_DATE_FORMAT_WITHOUT_YEAR;
	}

	return Intl.DateTimeFormat(
		document.documentElement.lang || 'en',
		dateFormat
	).format( date );
}

/**
 * Removes the given number of days from a "YYYY-MM-DD" string, and returns
 * the result in the same format.
 *
 * @param {Date}   date The date to be processed.
 * @param {number} days The number of days to remove from the date.
 * @return {string} The resulting date in "YYYY-MM-DD" format.
 */
export function removeDaysFromDate( date: Date, days: number ): string {
	if ( false === Number.isInteger( days ) ) {
		return __( 'days parameter must be an integer', 'wp-parsely' );
	}

	if ( false === canProcessDate( date ) ) {
		return DATE_NOT_AVAILABLE_MESSAGE;
	}

	const pastDate = new Date( date );
	pastDate.setDate( pastDate.getDate() - days );

	return convertDateToString( pastDate );
}

/**
 * Converts a date to a string in "YYYY-MM-DD" format.
 *
 * @param {Date} date The  date to format.
 * @return {string} The date in "YYYY-MM-DD" format.
 */
export function convertDateToString( date: Date ): string {
	if ( false === canProcessDate( date ) ) {
		return DATE_NOT_AVAILABLE_MESSAGE;
	}

	return date.toISOString().substring( 0, 10 );
}

```
