Polymer - 绑定值中的格式日期

Polymer - format date in binded value

我在模板中有一个磁贴,我希望它显示日期:

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

其中date是元素的属性:

  date: {
    type: Date,
  }

然而,这将显示整个日期和时间,而我只想要日期。所以我想做的是:

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

但这行不通。如何在此设置中格式化日期?

为此,您可以在 px-tile.html 中安排如下内容:

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))
   }
}

保持父元素与您之前的示例相同,但将属性名称更改为 date,这将是 属性 在子元素:

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