Skip to content

Commit 054c97c

Browse files
committed
Add useWpLiveDate hook
1 parent d95d023 commit 054c97c

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { date } from '@wordpress/date';
2+
import { useEffect, useMemo, useState } from 'react';
3+
4+
// "July 23, 2025 10:19:45 am"
5+
const defaultFormat = 'F j, Y g:i:s a';
6+
7+
export function useWpLiveDate(dateFormat = defaultFormat) {
8+
const [key, setKey] = useState(0);
9+
10+
useEffect(() => {
11+
const interval = setInterval(() => setKey((prev) => prev + 1), 1000);
12+
13+
return () => clearInterval(interval);
14+
}, []);
15+
16+
return useMemo(() => {
17+
// This is to force re-render when key changes
18+
// And to avoid lint warnings about dependency not used
19+
key.toString();
20+
21+
return date(dateFormat, undefined, undefined);
22+
}, [key, dateFormat]);
23+
}

packages/js/utilities/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"module": "index.ts",
88
"dependencies": {
99
"@paralleldrive/cuid2": "^2.2.2",
10+
"@wordpress/date": "^5.26.0",
1011
"@wpsocio/i18n": "workspace:*",
1112
"copy-to-clipboard": "^3.3.3",
1213
"ramda": "^0.30.1"

pnpm-lock.yaml

Lines changed: 11 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)