# Change a date from 2 inputs, binding two-way recursive-error

**URL:** <https://discourse.aurelia.io/t/change-a-date-from-2-inputs-binding-two-way-recursive-error/1773>\
**Category:** Help Requests\
**Created:** [October 17, 2018, 2:12pm UTC](https://discourse.aurelia.io/t/change-a-date-from-2-inputs-binding-two-way-recursive-error/1773 "2018-10-17T14:12:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreaProgrammerIt](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@AndreaProgrammerIt](https://discourse.aurelia.io/u/AndreaProgrammerIt)\
**Post date:** [October 17, 2018, 2:12pm UTC](https://discourse.aurelia.io/t/change-a-date-from-2-inputs-binding-two-way-recursive-error/1773/1 "2018-10-17T14:12:37Z")

</div>

Hi all,

anyone know why when i set a date from moment and “this.test” (my variable") and it’s also binded in the view it crashs?

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/a/ad6efdb1ae6f14090128a31136edd87fda9f2515.png)

Thank you all,  
Andrea

---

<div class="post-metadata">

**Author:** ![AndreaProgrammerIt](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@AndreaProgrammerIt](https://discourse.aurelia.io/u/AndreaProgrammerIt)\
**Post date:** [October 17, 2018, 2:34pm UTC](https://discourse.aurelia.io/t/change-a-date-from-2-inputs-binding-two-way-recursive-error/1773/2 "2018-10-17T14:34:39Z")

</div>

I got it.  
There is a problem with the binding.

value.two-way became recursive but :

how can do change a date from input dialog or another input( nights)

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/8/8b3b4fe35d4ad468535d25400e2c9eb4c1ae5f5d.png)

---

<div class="post-metadata">

**Author:** ![AndreaProgrammerIt](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@AndreaProgrammerIt](https://discourse.aurelia.io/u/AndreaProgrammerIt)\
**Post date:** [October 17, 2018, 2:35pm UTC](https://discourse.aurelia.io/t/change-a-date-from-2-inputs-binding-two-way-recursive-error/1773/3 "2018-10-17T14:35:54Z")

</div>

If i change night the “DateEnd” change correctly but the control is value.two-way so it continues for a loop to raise the property and go in error…

How can set the date so?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [October 17, 2018, 11:13pm UTC](https://discourse.aurelia.io/t/change-a-date-from-2-inputs-binding-two-way-recursive-error/1773/4 "2018-10-17T23:13:11Z")

</div>

You will need some guard condition to prevent syncing, if you can provide a repro of what you want to achieve, it will be easier for folks to explain and help.

---

<div class="post-metadata">

**Author:** ![davismj](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/davismj/32/267_2.png) [@davismj](https://discourse.aurelia.io/u/davismj)\
**Post date:** [October 25, 2018, 3:41am UTC](https://discourse.aurelia.io/t/change-a-date-from-2-inputs-binding-two-way-recursive-error/1773/5 "2018-10-25T03:41:11Z")

</div>

The right way to solve this is using a value converter that has a single object for the date.

```javascript
export class App {
  date = new Date();
}

export class AsDateValueConverter {
  
  // we store the value to capture current state
  value = new Date();
  
  // to view expects a Date object
  toView(date) {
    
    // if the value exists and is different from the current value
    if (date && !this.equal(date, this.value)) {
      
      // we update the current value
      this.value = date;
    }
    
    // and we pass the value of the current value (not the incoming value) as a string to the view
    console.log('toView', this.toString(this.value));
    return this.toString(this.value);
  }
  
  // fromView expects a date string
  fromView(dateString) {
    
    // we parse the date
    const date = new Date(dateString);
    
    // if the date is valid and different from the current value
    if (!isNaN(date.getTime()) && !this.equal(date, this.value)) {
      
      // we update the current value
      this.value = date;
    }
    
    // and we pass the value of the current value to the view model
    console.log('fromView', this.value)
    return this.value;
  }
  
  // we define a custom function for determining how the value should be displayed in the view
  toString(date) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    return `${year}-${month < 10 ? '0' : ''}${month}-${day < 10 ? '0' : ''}${day}`;
  }
  
  // we test for equality based on what results in a similar view to prevent infinite loops
  equal(dateOne, dateTwo) {
    return this.toString(dateOne) === this.toString(dateTwo);
  }
}

```

Running gist here: [https://gist.run/?id=040775f06aba5e955afd362ee60863aa](https://gist.run/?id=040775f06aba5e955afd362ee60863aa)
