0

I'm having a parent LWC from where I'm passing a deliveryYear value to Child LWC

In parent I'm getting deliveryYear as 2023/2024 from which I'm splitting the first year and sending to child.

enter image description here

Parent.js

var del = this.deliveryYear.split("/");
this.splitDelYear = del[0];

Parent.html

<c-child deliveryyear={splitDelYear}></c-child>

In child LWC I'm capturing this value and trying to build a min and max date range from it. Used this date range in my html but it is not throwing error when selecting date outside this range.

Child.js

@api deliveryyear;

delyearstartdate;
delyearenddate;
delyearenddate=new Date(this.deliveryyear1+1,8,30);
delyearstartdate = new Date(this.deliveryyear1,9,1);
startdatemin=this.formatDate(this.delyearstartdate);
enddatemax=this.formatDate(this.delyearenddate);

Child.html

<lightning-input type="date" onchange={handleChange}  class="selector" min={startdatemin} disabled={disabled} ></lightning-input>

<lightning-input type="date" onchange={handleChange}  class="selector" min={enddatemax} disabled={disabled} ></lightning-input>

This is working fine if I'm hard-coding a value for deliveryyear in child component itself

child.js

deliveryyear1=2023;

delyearstartdate;
delyearenddate;
delyearenddate=new Date(this.deliveryyear1+1,8,30);
delyearstartdate = new Date(this.deliveryyear1,9,1);
startdatemin=this.formatDate(this.delyearstartdate);
enddatemax=this.formatDate(this.delyearenddate);

enter image description here

Any inputs on why it is not formatting the value that is coming from parent component.

Both are showing same result when printed.

enter image description here

1 Answer 1

0

I found a solution myself. So instead of splitting the year in the parent split the year in child and then convert it into integer to format it as required.

child.js: @api this.deliveryyear (which is "2023/2024")

In connected call back I handled the split and date conversion

        var del = this.deliveryyear.split("/");
        this.splitDelYear = Number(del[0]);
        this.delyearenddate=new Date(this.splitDelYear+1,8,30);
        this.delyearstartdate = new Date(this.splitDelYear,9,1);
        this.startdatemin=this.formatDate(this.delyearstartdate);
        this.enddatemax=this.formatDate(this.delyearenddate);

You must log in to answer this question.

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