| 1 |
import {dateI18n, getDate} from '@wordpress/date'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Format the date to a date-local input compatible string |
| 5 |
* @since 4.13.0 |
| 6 |
*/ |
| 7 |
export default function formatToDateLocalInput(dateString: string) { |
| 8 |
if (!dateString) { |
| 9 |
return ''; |
| 10 |
} |
| 11 |
|
| 12 |
// Interpret server-provided naive strings as site timezone (WordPress timezone), |
| 13 |
// and preserve the wall time for the date input. |
| 14 |
const dateObj = getDate(dateString); |
| 15 |
if (isNaN(dateObj.getTime())) { |
| 16 |
return ''; |
| 17 |
} |
| 18 |
|
| 19 |
return dateI18n('Y-m-d', dateObj, undefined); |
| 20 |
} |
| 21 |
|