2

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?

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

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 ;
    }

1 Answer 1

1

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.

1
  • 1
    Assigning directly as you suggested "event.target.value=firstdate; " solved the issue. My approach which worked partially was assigning track variable to the value on the template as one would normally do in an LWC. Issue for that could be how SF internally handles it ( timing wise ).
    – vino
    Commented Apr 10, 2020 at 14:22

You must log in to answer this question.

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