0

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

| improve this answer | |
  • 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 ). – vinod Apr 10 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.