Lightning Web Components – Set Locale Time for Validation in lightning-input

javascriptjavascript-controllerlightning-design-systemlightning-web-componentslwc-oss

.html snippet (LWC Component)

enter image description here

.js snippet (LWC Component)

import { LightningElement, track } from 'lwc';

export default class DateTimePickerLightningInput extends LightningElement {

@track today;

connectedCallback(){

    this.today = new Date().toLocaleString();

}

}

Outcome:

enter image description here enter image description here

As per the above outcome, the Current Locale DateTime = 10/19/2022, 5:50:37 PM. But, if you could see that the lightning-input with type=datetime component is considering the DateTime = Oct 19, 2022, 9:50:37 AM and thus the validation message is dependent on the DateTime Value = Oct 19, 2022, 9:50:37 AM.

Is there any way for the lightning-input component to consider the Current Locale DateTime = 10/19/2022, 5:50:37 PM based on the User Location / Region Timezone?

Thanks in Advance !!!

Outcome using Date().toISOString() as below:

enter image description here enter image description here

Best Answer

Try using this -

connectedCallback(){

    this.today = new Date().toISOString();

}
Related Topic