+ Start a Discussion
rushirushi 

lwc reactivity

Hello All,

I have created a LWC component and part of that created new dates fields. In Js, I wrote the logic that when the selected date is below today's date then assign it to today's date and by default date should be today's date.

Issue: when the selected date is less than today's date then JS logic is executing and updating it to today's date but in UI it's not reflecting.
Example: When the page is loaded dafult date will be 4/27/2022 and when I select the date 4/20/2022, UI will display 4/20/2022 value and JS will hold the 4/27/2022 value.

<lightning-input type="date" data-id="date1" variant="label-hidden" placeholder="Enter Start Date.." value={date1} onchange={handleDateChange} ></lightning-input>

handleDateChange(event) {
  let val = event.target.value;
  const changedDate = val;
  if(changedDate < this.today) 
     this.date1 = this.today;
}

 
mukesh guptamukesh gupta
HI Rushi,

Please use below code:-
 
<lightning-input type="date" data-id="date1" variant="label-hidden" placeholder="Enter Start Date.." value={date1} onchange={handleDateChange} ></lightning-input>

handleDateChange(event) {
  let val = event.target.value;
  var Today = new Date();
  var changedDate = new Date(val);   
  if(selectedDt.getTime() < Today.getTime()) 
     this.date1 = this.today;
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh