4

I have a tile inside a template and I want it to show a date:

  <template>
    <px-tile
        description=[[date]]
    </px-tile>
  </template>

Where date is a property of the element:

  date: {
    type: Date,
  }

This will however display the whole date and time, and I only want the date. So I'm trying to do:

  <template>
    <px-tile
        description=[[date.toLocaleDateString("en-US")]]
    </px-tile>
  </template>

But this doesn't work. How can I format the date in this setup?

lte__
  • 7,175
  • 25
  • 74
  • 131
  • A concise way of doing this is using a computed function like `description="[[formatDate(date)]]"` and define `formatDate` in that same element. [Here's](https://www.polymer-project.org/2.0/docs/devguide/data-binding#annotated-computed) the Polymer docs on this – Garrett Oct 17 '18 at 07:24

1 Answers1

0

For this you could arrang in px-tile.html something like:

DEMO

...
</style>
<div>Date : [[localDate]]</div>
...

  date: {
    type: Date,
    observer:'_checkDate'
  }


_checkDate(d) {
   if (d) {
         // you may set the options with this information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
         var options = { year: 'numeric', month: 'long', day: 'numeric' }; 
         this.set('localDate', d.toLocaleDateString('en-US', options))
   }
}

Keep the parent element same as your previous sample but change the attribute name as date that this will be property at child element:

 <template>
    <px-tile
        date=[[date]]>
    </px-tile>
  </template>
Cappittall
  • 3,300
  • 3
  • 15
  • 23