I want to compare my two dates, start date with end date, if start date >= end date so I can save if not I can't save, the problem I have is that in the console I get only the value of the end date and the start date is undefined , so always my condition goes to the else . How can I correct this?

==> JS

        if (this.EndDate >= this.StartDate) {
            console.log('EndDate  > StartDate');
            console.log(' date fin est '+this.EndDate );
            console.log('date début'+this.StartDate);
            this.EndDate = event.target.value;
            this.Submit = true;

        else {
            console.log('EndDate< StartDate');
            console.log('valeur date'+this.EndDate);
            console.log('date début'+this.StartDate);
            this.EndDate= event.target.value;
            this.Submit = false;



==> HTML

                        <lightning-input-field field-name="Start_Date__c" required onchange={CompareDate}   value={StartDate}>
                        <lightning-input-field field-name="EndDate__c" required  onchange={CompareDate} value={EndDate}>
  • @sfdcfox I Used this code in the case : case 'Start_Date__c': to get just to get the Today Date not also the Date Time but I get the correct forma of Date but my condition go automatically to the Case number 2 and give me that the start date is undefined let rightNow = new Date(); rightNow.setMinutes( new Date().getMinutes() - new Date().getTimezoneOffset() ); this.startDate = rightNow.toISOString().slice(0,10); console.log(startDate); Apr 18, 2022 at 13:09

1 Answer 1


Data bindings are one-way in LWC, so you have to set the value in the variables first:

if(event.target.fieldName === 'Start_Date__c') {
  this.StartDate = event.target.value;
} else if(event.target.fieldName === 'End_Date__c') {
  this.EndDate = event.target.value;
this.Submit = this.StartDate && this.EndDate && this.StartDate < this.EndDate;

I wrote a demo app that works in my org.

import { LightningElement } from 'lwc';
export default class StartEndDemo extends LightningElement {
    startDate = new Date().toISOString();
    get cannotSubmit() {
        return !this.submit;
    handleChange(event) {
        switch( event.target.fieldName) {
            case 'Start_Date__c': {
                this.startDate = event.target.value;
            case 'End_Date__c': {
                this.endDate = event.target.value;
        this.submit = this.startDate && this.endDate && this.startDate < this.endDate;

    <lightning-record-edit-form object-api-name="Account">
        <lightning-input-field field-name="Start_Date__c" onchange={handleChange} value={startDate}></lightning-input-field>
        <lightning-input-field field-name="End_Date__c" onchange={handleChange} value={endDate}></lightning-input-field>
        <lightning-button type="submit" label="Save" disabled={cannotSubmit}></lightning-button>
    Start: {startDate}<br/>
    End: {endDate}
  • I get an error with this code This page contains an error. Try updating it. To start, would you like to give us some information? (we designate this error with the ID: -455474642) Apr 17, 2022 at 14:45
  • @salesforcedev Hmm, I made a demo in my org, it works correctly. Please review my code and compare it to your own.
    – sfdcfox
    Apr 17, 2022 at 15:51
  • thank you for your help , Yes now it works, the problem I had and I just discovered is that the start date is automatically fed by today's date, so if I don't click it considers it as indefinite , does this have a solution? Apr 18, 2022 at 9:52
  • @salesforcedev Yes, you can initialize the value to whatever you desire in a number of ways. Let me write something up.
    – sfdcfox
    Apr 18, 2022 at 10:34
  • Yes , If you can help I will be Grateful Apr 18, 2022 at 10:40

You must log in to answer this question.

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