[SalesForce] LWC lightning-input type=”date” setting value and changing value dynamically with modified date

I'm able to change and show the modified value of date on a lightning-input type="date" LWC component . My JS code will calculate and show the first date of the month for any date selection of the month using date picker. Perfect. When I change the date again using the date picker FOR THE SAME MONTH, it will not show the first date as value. But using date picker if I pick another month and date it will show the first month, which is good. But again for this month also if I repeat and change date using date picker it will show the selected date only, even though on console.log I can see the value of {startDtToFirstOfMonth} as the first month always. Very strange. Any clues?

<pre>
lightning-input value={startDtToFirstOfMonth} type="date" name="stDt" label="Enter a date" 
onchange={handleStDateCahngeEvent} 
</pre>
<pre>
/lightning-input    
</pre>

JS

@track startDt;
    @track startDtToFirstOfMonth;

    handleStDateCahngeEvent(event){
      this.startDt = event.target.value;
      this.startDtToFirstOfMonth = this.getFirstDateOfMonth(this.startDt) ; 
    }

    getFirstDateOfMonth(dt){
        const regex = /(\-\d\d)$/;          
        var givenDateConvertedToFirst = dt.replace(regex,'-01');          
        return givenDateConvertedToFirst ;
    }

Best Answer

You can change the target value to behave in this way by doing following

handleStDateCahngeEvent(event){
this.startDt = event.target.value;
var firstdate=this.getFirstDateOfMonth(this.startDt);
event.target.value=firstdate;
this.startDtToFirstOfMonth=firstdate;

}

Though i dont know how it changed the first time and then didn;t worked just for the same month.

Related Topic