Skip to content

Commit 718af07

Browse files
committed
Fix client-side formatting of the date field
1 parent c57b271 commit 718af07

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

assets/src/js/bindings/sources.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { registerBlockBindingsSource } from '@wordpress/blocks';
55
import { store as coreDataStore } from '@wordpress/core-data';
6-
import { dateI18n } from '@wordpress/date';
6+
import { format } from '@wordpress/date';
77

88
/**
99
* Get the value of a specific field from the SCF fields.
@@ -88,7 +88,18 @@ const processFieldBinding = (
8888
if ( ! fieldValue ) {
8989
return '';
9090
}
91-
return dateI18n( fieldConfig?.display_format, fieldValue ) || '';
91+
/**
92+
* On the server side, we use `date_i18n()` (PHP) to format dates, see
93+
* https://developer.wordpress.org/reference/functions/date_i18n/.
94+
* However, the client-side (JS) version, `dateI18n()`, seems to have a bug
95+
* that gets the timezone wrong. When the WordPress install's timezone is set
96+
* to UTC, and the client timezone is UTC+x, it will return the _previous_ day.
97+
* This is probably because a date without a time is treated as midnight UTC,
98+
* which is still the previous day in UTC+x timezones (i.e. east of Greenwich).
99+
* Since we aren't interested in times and timezones for date picker fields,
100+
* we can simply use the `format()` function to format the date.
101+
*/
102+
return format( fieldConfig?.display_format, fieldValue ) || '';
92103
case 'image':
93104
// fieldValue is a (numeric) image ID.
94105
const imageObj = getMedia( fieldValue );

0 commit comments

Comments
 (0)