# wp-parsely/3.7.1/src/blocks/shared/utils/date.ts

Parse.ly, version 3.7.1. 55 lines.

- Page: https://pluginprobe.com/plugins/wp-parsely/3.7.1/code/src/blocks/shared/utils/date.ts
- Raw: https://pluginprobe.com/plugins/wp-parsely/3.7.1/raw/src/blocks/shared/utils/date.ts
- Modified: 2023-02-27T12:36:48+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.7.1/code/src/blocks/shared/utils/date.ts#L10-L20`.

```typescript
export const SHORT_DATE_FORMAT: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' };
export const SHORT_DATE_FORMAT_WITHOUT_YEAR: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric' };

export function getDateInUserLang( date: Date, options: Intl.DateTimeFormatOptions ): string {
	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 {
	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 {string} date The date in "YYYY-MM-DD" format.
 * @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: string, days: number ): string {
	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 {
	return date.toISOString().substring( 0, 10 );
}

```
