# Example: Building a date input field

**URL:** <https://discuss.emberjs.com/t/example-building-a-date-input-field/674>\
**Category:** Uncategorized\
**Created:** [March 21, 2013, 11:44pm UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674 "2013-03-21T23:44:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahx/32/14322_2.png) [@ahx](https://discuss.emberjs.com/u/ahx)\
**Post date:** [March 21, 2013, 11:44pm UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674/1 "2013-03-21T23:44:44Z")

</div>

Here is an example for a input field with type=“date”. The goal/problem was to bind the value of the input field directly to a non-String value. In this case it’s a normal Date object.

[http://jsbin.com/ulapuf/2/edit](http://jsbin.com/ulapuf/2/edit)

The gist:

```
App.DateField = Ember.TextField.extend({
  type: 'date',
  valueBinding: 'dateValue',
  dateValue: (function(key, value) {
    if (value) {
      return this.set('date', new Date(value));
    } else {
      return (this.get('date') || new Date()).toISOString().substring(0, 10);
    }
  }).property('date')
});

```

It looks like sometimes there are several ways to implement stuff like this in Ember. Can you come up with another way to do this (better or worse)?

I just want to learn about different ways to do stuff.

---

<div class="post-metadata">

**Author:** ![seilund](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/seilund/32/14351_2.png) [@seilund](https://discuss.emberjs.com/u/seilund)\
**Post date:** [March 22, 2013, 5:44am UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674/2 "2013-03-22T05:44:04Z")

</div>

> [@ahx](#):
>
> Can you come up with another way to do this (better or worse)?

Yes, better, or at least simpler:

```javascript
App.DateField = Ember.TextField.extend({
  type: 'date',
  date: function() {
    var value = this.get('value');
    if (value) {
      return new Date(value)); //You probably want to validate the date in some way
    } else {
      return null;
    }
  }.property('value')
});

```

---

<div class="post-metadata">

**Author:** ![ahx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahx/32/14322_2.png) [@ahx](https://discuss.emberjs.com/u/ahx)\
**Post date:** [March 22, 2013, 8:45am UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674/3 "2013-03-22T08:45:39Z")

</div>

Nah, this way the binding does not work both ways. Or does it? [http://jsbin.com/ulapuf/4/edit](http://jsbin.com/ulapuf/4/edit)

---

<div class="post-metadata">

**Author:** ![seilund](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/seilund/32/14351_2.png) [@seilund](https://discuss.emberjs.com/u/seilund)\
**Post date:** [March 22, 2013, 9:08am UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674/4 "2013-03-22T09:08:59Z")

</div>

Missed that you wanted to be able to set `date`. Then try this:

```javascript
App.DateField = Ember.TextField.extend({
  type: 'date',
  date: function(key, date) {
    if (date) {
      this.set('value', date.toISOString().substring(0, 10));
    } else {
      value = this.get('value');
      if (value) {
        date = new Date(value);
      } else {
        date = null;
      }
    }
    return date;
  }.property('value')
});

```

See example at [http://jsbin.com/ulapuf/6/edit](http://jsbin.com/ulapuf/6/edit)

---

<div class="post-metadata">

**Author:** ![ahx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahx/32/14322_2.png) [@ahx](https://discuss.emberjs.com/u/ahx)\
**Post date:** [March 22, 2013, 9:19am UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674/5 "2013-03-22T09:19:08Z")

</div>

Sweet! Using the date method directly feels much better. Thanks.

---

<div class="post-metadata">

**Author:** ![ahx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahx/32/14322_2.png) [@ahx](https://discuss.emberjs.com/u/ahx)\
**Post date:** [March 31, 2013, 1:13am UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674/6 "2013-03-31T01:13:38Z")

</div>

Here’s is another version.

I wanted something that does only change the date if the text value can be parsed by Date.parse(). As a side effect, now you can also type “2012” and it yields to “2012-01-01” on blur, which is kinda cool. – Of course this only comes into effect on browsers that do not support date input fields (like Firefox).

[http://jsbin.com/epadin/1/edit](http://jsbin.com/epadin/1/edit)

---

<div class="post-metadata">

**Author:** ![pascalzajac](https://avatars.discourse-cdn.com/v4/letter/p/a5b964/32.png) [@pascalzajac](https://discuss.emberjs.com/u/pascalzajac)\
**Post date:** [November 25, 2013, 6:37am UTC](https://discuss.emberjs.com/t/example-building-a-date-input-field/674/7 "2013-11-25T06:37:04Z")

</div>

This worked perfectly for me, thanks for the guide.
