Magento 2.1 – Adjust Timezone in DateTime UI Component

datetimemagento-2.1uicomponent

I created a time field by the Q&A Magento 2 – How to add the DateTime UI Component, and I also set the store TimeZone is "Asia/Shanghai". But when I click the "Go Today", it always show the time of UTC timezone. How to fix it?

datetime UI Componet

Best Answer

The timezone is right. The origin of the problem is the wrong timeFormat in the answer of Magento 2 - How to add the DateTime UI Component, We need change hh:mm:ss to HH:mm:ss in the UI Componet, otherwise 03:00:00 PMand 03:00:00 AM will became the same time, lack 12 hours and you can not save the time of PM in DB table.


Below is the original answer from Magento 2 - How to add the DateTime UI Component, we need change <item name="timeFormat" xsi:type="string">hh:mm:ss</item> to <item name="timeFormat" xsi:type="string">HH:mm:ss</item>.

<field name="start_date">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">string</item>
            <item name="label" xsi:type="string" translate="true">Go Live Start Date</item>
            <item name="formElement" xsi:type="string">date</item>
            <item name="source" xsi:type="string">page</item>
            <item name="sortOrder" xsi:type="number">21</item>
            <item name="dataScope" xsi:type="string">start_date</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
            <item name="options" xsi:type="array">
                <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                <item name="timeFormat" xsi:type="string">hh:mm:ss</item>
                <item name="showsTime" xsi:type="boolean">true</item>
            </item>
        </item>
    </argument>
</field>
Related Topic