File size: 792 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { __, sprintf } from '@wordpress/i18n';
import { useLocale } from '../../app/locale';
import { formatDate } from '../../utils/datetime';

export function useFormattedTime( timestamp: string, formatOptions?: Intl.DateTimeFormatOptions ) {
	const locale = useLocale();

	const date = new Date( timestamp );
	const formatted = formatDate( date, locale, formatOptions );

	if ( ! formatOptions?.dateStyle ) {
		const now = new Date();

		const isToday =
			date.getDate() === now.getDate() &&
			date.getMonth() === now.getMonth() &&
			date.getFullYear() === now.getFullYear();

		if ( isToday ) {
			// translators: time today
			return sprintf( __( 'Today at %s' ), formatted );
		}
		return formatDate( date, locale, { ...formatOptions, dateStyle: 'long' } );
	}

	return formatted;
}