3

I'm using lightning-input type="datetime" to take datetime input from a user inside a lightning web component. I initially bind the value returned from back-end to the input. It displays the correct date-time value fetched from back-end on the date-time picket.

However, when I call a js method on change of input, it fetches incorrect value.

So, in my case I'm displaying 12:00 PM to the user initially (fetched from back-end sfdc record). Now, if user updates the value to 12:15 PM, the handler updates the value on change. I use console to see the updated value and it displays 19:15 instead of 12:15 PM.. any idea why?

Below is the code snippet:

    <lightning-input type="datetime"
                                    label="Last Date"
                                    value={endDate}
                                    onchange={captureDateTimeChange}>
                    </lightning-input>

JS:

captureDateTimeChange(event){
    console.log('DateTime : ' ,event.target.value);
}

Selected DateTime from datetime picker

enter image description here

Captured Date in js method:

enter image description here

What's causing the js method to pick incorrect date ?

0

1 Answer 1

1

It is converting to UTC time. Whatever shown to you is depends on user's locale. E.g. Users locale is set to GMT +5:00, he'll see 5:00 PM, where as time is stored as 12:00PM.

Please find the text from documentation -

A datetime field includes a text input to type a date and a date picker to select a date, and similar fields for typing or picking a time. Your Salesforce locale setting determines the acceptable date and time formats. Your Salesforce language setting determines the names of months and weekdays that the date picker displays.

The date and time you enter are automatically validated against your Salesforce locale format during the blur event. The date and time displayed reflect your Salesforce time zone setting. Use the timezone attribute to specify a different time zone in IANA time zone database format. For example, specify timezone="America/New_York" for US Eastern Time or timezone="GMT" for Greenwich Mean Time.

5
  • 1
    in that case, shouldn't it display the date/time in datetime picker as per the locale? Why is there a discrepancy in what's displayed inside datetime picker and what is actually shown in console?
    – sfdcnoob
    Aug 7, 2020 at 11:10
  • No, on UI it'll show the locale but when you save it, it'll saved as UTC.
    – Ysr Shk
    Aug 7, 2020 at 11:11
  • So, that'd lead to Erroneous data being stored in the back-end. Is there a way I can convert it back to system time zone?
    – sfdcnoob
    Aug 7, 2020 at 11:21
  • @sfdcnoob If you're using the appropriate date/datetime methods, the data saved should be correct. We'd have to see more code related to your problem to help you. Please read about the X-Y Problem.
    – sfdcfox
    Aug 7, 2020 at 19:18
  • Hi @sfdcnoob because of the terrible design of the lightning-input defaulting to the salesforce timezone (seriously, who came up with that?) you can set the timezone attribute to new Date().toLocaleTimeString('en-us', { timeZoneName: 'short' }).split(' ')[2]; to get the abbreviated time
    – Andy Ray
    Jan 28, 2022 at 1:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .