1

I'm using the following code to get and set a date meta value for a CPT in Gutenberg (some code omitted for brevity):

const [meta, setMeta] = useEntityProp(
    'postType',
    'activity',
    'meta'
);

const startDate = meta['_my_start_date'];

function onUpdateStartDate(value) {
    setMeta({ ...meta, '_my_start_date': value });
}

...

<PanelRow className="edit-activity-start-date">
    <span>{__('Activity Start Date', 'my-domain')}</span>
    <Dropdown
        placement="middle left"
        renderToggle={(({ isOpen, onToggle }) => (
            <Button isLink={true} onClick={onToggle} aria-expanded={isOpen}>
                {startDate ? dateI18n('j F, Y', startDate) : __('Pick a date', 'my-domain')}
            </Button>
        ))}
        renderContent={() => (
            <DatePicker
                currentDate={startDate}
                onChange={(newDate) => onUpdateStartDate(newDate)}
                startOfWeek={1}
            />
        )}>
    </Dropdown>
</PanelRow>

The thing is that the DatePicker component also stores the current time for the selected date (example: 2023-11-09T11:12:06). Is there a way to set the time in the DatePicker component to ZERO?

1 Answer 1

0

I managed to find the solution myself. You need to modify the onUpdateStartDate() function:

function onUpdateStartDate(value) {
    const date = new Date(value);
    date.setHours(0, 0, 0, 0);
    setMeta({ ...meta, '_my_start_date': date });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.