3

I already set my timezone config to GMT+7 in backend , and i've created a custom module which will show grid in backend that has date column, the problem is the date is being save in utc format, and i need to show it in GMT+7 format through grid:

    <column name="created_at">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">dateRange</item>
                <item name="label" translate="true" xsi:type="string">Created at</item>
            </item>
        </argument>
    </column>
1
  • check this link
    – Hitesh
    Oct 31, 2018 at 5:26

1 Answer 1

7

Add class in date filed to format date according to time format. use this code.

 <column name="creation_time" class="Magento\Ui\Component\Listing\Columns\Date">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">dateRange</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                <item name="dataType" xsi:type="string">date</item>
                <item name="label" xsi:type="string" translate="true">Created at</item>
            </item>
        </argument>
    </column>

it should work because in class Magento\Ui\Component\Listing\Columns\Date prepareDataSource() it is converting date.

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            if (isset($item[$this->getData('name')])) {
                $date = $this->timezone->date(new \DateTime($item[$this->getData('name')]));
                $timezone = isset($this->getConfiguration()['timezone'])
                    ? $this->booleanUtils->convert($this->getConfiguration()['timezone'])
                    : true;
                if (!$timezone) {
                    $date = new \DateTime($item[$this->getData('name')]);
                }
                $item[$this->getData('name')] = $date->format('Y-m-d H:i:s');
            }
        }
    }

    return $dataSource;
}

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.