Skip to content

Magento 2 : Ui component displaying wrong date

So I created a module where I am storing order details in custom table and later on displaying all the details in custom grid.
Everything is working fine except for date field.

In the database its entering correct date but in the grid its showing previous day date.
I checked my local date, dateformat but nothing works.

My observer which is saving the dates in the database.

if ($this->isEnabled()) {
            $order = $observer->getEvent()->getOrder();
            $customerId = $order->getCustomerId();
            $customerEmail = $order->getCustomerEmail();
            $referralEmail = $this->getAttributeValue($customerId);
            if (!empty($referralEmail)) {
                $ordernumber = (int)$order->getIncrementId();
                $orderTotal = $order->getSubtotal();
                $discountAmount= $order->getDiscountAmount();
                $totalAmount= $orderTotal - abs($discountAmount);
                $orderDate = date('m/d/Y');
                $checkDate = date('Y-m-d', strtotime("+2 months", strtotime($orderDate)));
                $status = 'Not Paid';
                $customerOrderData = $this->customerDataFactory->create();
                $customerOrderData->addData(['purchase_date' => $orderDate, 'amount' => $totalAmount, 'customer_id' => $customerId, 'customer_email' => $customerEmail, 'referrer_email' => $referralEmail, 'order_number' => $ordernumber, 'check_date' => $checkDate, 'status' => $status]);
                $customerOrderData->save();
            }
        }

My xml which is displaying the grid.

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">rewards_listing.rewards_listing_data_source</item>
            <item name="deps" xsi:type="string">rewards_listing.rewards_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">spinner_columns</item>
    </argument>
    <listingToolbar name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sticky" xsi:type="boolean">true</item>
            </item>
        </argument>
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>

        <filters name="listing_filters" />
        <paging name="listing_paging"/>
        <exportButton name="export_button"/>
        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                    <item name="indexField" xsi:type="string">entity_id</item>
                    <item name="displayArea" xsi:type="string">bottom</item>
                    <item name="SelectProvider" xsi:type="string">rewards_listing.rewards_listing.rewards_columns.ids</item>
                </item>
            </argument>
            <action name="update">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">update</item>
                        <item name="label" xsi:type="string" translate="true">Update</item>
                        <item name="url" xsi:type="url" path="rewards/friendsandfamily/massUpdate"/>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Update Posts</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure you want to update selected items?</item>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>
    </listingToolbar>
    <dataSource name="nameOfDataSource">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">MagentoFrameworkViewElementUiComponentDataProviderDataProvider</argument>
            <argument name="name" xsi:type="string">rewards_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">entity_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">entity_id</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <columns name="spinner_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">entity_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="entity_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>

         <column name="purchase_date" class="MagentoUiComponentListingColumnsDate">
            <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">Purchase date</item>
<item name="dateFormat" xsi:type="string" translate="true">YYYY-MM-dd</item>
                </item>
            </argument>
        </column>

        <column name="amount">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">Amount</item>
                </item>
            </argument>
        </column>
        <column name="customer_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">Customer ID</item>
                </item>
            </argument>
        </column>
        <column name="customer_email">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">Customer Email</item>
                </item>
            </argument>
        </column>
        <column name="referrer_email">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">Referrer Email</item>
                </item>
            </argument>
        </column>
        <column name="order_number">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">Order Number</item>
                </item>
            </argument>
        </column>
        <column name="check_date" class="MagentoUiComponentListingColumnsDate">
            <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">Check date</item>
<item name="dateFormat" xsi:type="string" translate="true">YYYY-MM-dd</item>
                </item>
            </argument>
        </column>
        <column name="status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">Status</item>
                </item>
            </argument>
        </column>

    </columns>
</listing>

So suppose if I am making an order on 20th july 2021. In the database its going as 20th july 2021 but in the grid its showing 19th july 2021