1

I have two bootstrap datepickers that represent an interval of time( one being the start date and the other the end date). I have both of these dates binded with ngModel:

 <td>
                    {{'STARTDATE'|translate}}
                    <datepicker [(ngModel)]="StartDate" name="StartDate" [showWeeks]="false" (ngModelChange)="setEndDate()"></datepicker>
                </td>
                <td>
                    {{'ENDDATE'|translate}}
                    <datepicker [(ngModel)]="EndDate" name="EndDate" [showWeeks]="false" (ngModelChange)="setStartDate()"></datepicker>

                </td>

When I change the Start Date the End Date must be changed and vice versa, depending on a selected time interval(e.g: a day, a week, a month, etc). Dates are updated in the setEndDate and SetStartDate methods.

The problem is when one variable is changed the other one is changed twice. Any tips?

UPDATE: Here are the Date set functions:

setEndDate() {
    var datediffcardinal: number = this.checkDateDiff()
    this.EndDate = new Date()
    this.EndDate.setDate(this.StartDate.getDate() + datediffcardinal)
    console.log("End")
    console.log(this.EndDate)
}

setStartDate() {
    var datediffcardinal: number = this.checkDateDiff()
    this.StartDate = new Date();
    this.StartDate.setDate(this.EndDate.getDate() - datediffcardinal)
    console.log("Start")
    console.log(this.StartDate)
}

checkDateDiff() {
    var end = Date.parse(this.wallboard.endDate.toString())
    var start = Date.parse(this.wallboard.startDate.toString())
    var diff = end - start
    var days: number = Math.floor(diff / (1000 * 60 * 60 * 24)) + 1
    return days;
}

1 Answer 1

3

you need to choose between [(myProperty)] and [myproperty]+(mypropertyChange) combo.

this is wrong :

<datepicker [(ngModel)]="StartDate" name="StartDate" [showWeeks]="false" (ngModelChange)="setEndDate()"></datepicker>

this is good :

<datepicker [ngModel]="StartDate" name="StartDate" [showWeeks]="false" (ngModelChange)="updateStartDate($event)"></datepicker>
updateStartDate(date){
    this.StartDate = date;
    this.setEndDate();
}
Sign up to request clarification or add additional context in comments.

6 Comments

does not work. Date is not updated in the other datepicker.
It's not supposed to work out of the box. I don't know your code.
code is already updated with setStartDate and setEndDate. These are the only references to the variables.
I edited my answer. You need to do the same with endDate
It does work, if something goes wrong, it comes from elsewhere... See that plnkr I made with numbers instead of date
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.