[SalesForce] Salesforce DataTable issue with timeZone of datetime field

Issue I am facing is I am querying a list of data. When I am displaying that data in dataTable, datable is showing date time field based on machine this code is running, not on timeZone assigned to user in salesforce. Like my Local is india/kolkata and Salesforce org user is in America/Los Angeles. So time displayed is india/kolkata not America/Los Angeles.

Here is my sample code

import { LightningElement, wire, track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class TestAccount extends LightningElement {
    @track data = [];
    col = this.columns = [{
        label: 'Name', 
        fieldName: 'Name', 
        type: 'text', 
        hideDefaultActions: true,
        sortable : true
    },{
        label: 'CreatedDate', 
        fieldName: 'CreatedDate', 
        type: 'date', 
        hideDefaultActions: true,
        typeAttributes:{
            year: "numeric",
            month: "long",
            day: "2-digit",
            hour: "2-digit",
            minute: "2-digit"
        }
    }];
    @wire(getRecord, { recordId: '0012D00000WYFgRQAX', fields: ['Account.Name','Account.CreatedDate']})
    getData({error,data}){
        if(data){
            this.data.push({Id:data.id, Name : data.fields.Name.value, CreatedDate : data.fields.CreatedDate.value});
        }
    }
}

html

<template>
    <lightning-datatable key-field="id" resize-column-disabled hide-checkbox-column data={data} columns={col} 
    ></lightning-datatable>
</template>

I have written above example to simplify code. I am actually doing an soql query to get records. This "data.fields.CreatedDate.value" is equal to value we get from soql. (Thats why I didn't used "data.fields.CreatedDate.displayValue");

Is there any parameter we can pass to show user's timezone in dataTable or this is excepted behaviour.

Best Answer

The documentation for lightning-datatable mentions the following

enter image description here

The locale set in the Salesforce user preferences determines the default formatting for date and time types. The data table supports the date object and dates provided as ISO8601 formatted strings.

This sounds like it should work. But, like you noted, it does use the computer's time zone setting to reflect the conversion of the datetime versus the Salesforce user settings.

I agree that the datatable doc isn't clear, but if you dig deeper you'll see the behavior is listed under lightning-formatted-date-time functions (which is used by datatable).

enter image description here

That's much clearer and matches the behavior of the component. It also shows there is a way to set the timezone to override this behavior.

Within the lightning-datatable doc, you can see there is a typeAttribute for timeZone that will help you achieve what you want (using @POZ's suggestion in combo).

enter image description here

<!-- get user selected timezone on user profile -->
import TIME_ZONE  from '@salesforce/i18n/timeZone';


{
        label: 'LastModifiedDate', 
        fieldName: 'LastModifiedDate', 
        type: 'date', 
        typeAttributes:{
            year: "numeric",
            month: "long",
            day: "2-digit",
            hour: "2-digit",
            minute: "2-digit",
            timeZone: TIME_ZONE <!-- set the timezone in the datetime column -->
        }
}