AMP
  • 网站

amp-date-display

简介

amp-date-display 组件以各种日期格式和不同的区域设置呈现日期信息。

设置

在头部包含 amp-date-display 组件...

<script async custom-element="amp-date-display" src="https://cdn.ampproject.org/v0/amp-date-display-0.1.js"></script>

...以及 amp-mustache 组件

<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>

基本用法

您必须指定 datetimetimestamp-mstimestamp-seconds 属性之一。这表示要显示的日期和时间。

日期信息通过 amp-mustache 模板呈现。

日、小时、分钟和秒值分别绑定到 {{day}}{{hour}}{{minute}}{{second}}

两位数格式变体({{dayTwoDigit}}{{hourTwoDigit}}{{minuteTwoDigit}}{{secondTwoDigit}})在个位数时用零填充显示这些值。其他值(如 {{iso}})也可用

<amp-date-display timestamp-seconds="2147483648" layout="fixed-height" height="20">
  <template type="amp-mustache">
      <a href="https://en.wikipedia.org/wiki/Year_2038_problem">Y2K38</a> will be at {{iso}}
  </template>
</amp-date-display>

可用变量

可以显示的所有变量都在下面的模板中。

<amp-date-display datetime="now" layout="fixed" width="360" height="590">
  <template type="amp-mustache">
    <table>
      <thead>
        <tr><th>variable</th><th>value</th></tr>
      </thead>
      <tbody>
        <tr><td>day</td><td>{{day}}</td></tr>
        <tr><td>dayName</td><td>{{dayName}}</td></tr>
        <tr><td>dayNameShort</td><td>{{dayNameShort}}</td></tr>
        <tr><td>dayPeriod</td><td>{{dayPeriod}}</td></tr>
        <tr><td>dayTwoDigit</td><td>{{dayTwoDigit}}</td></tr>
        <tr><td>hour</td><td>{{hour}}</td></tr>
        <tr><td>hour12</td><td>{{hour12}}</td></tr>
        <tr><td>hour12TwoDigit</td><td>{{hour12TwoDigit}}</td></tr>
        <tr><td>hourTwoDigit</td><td>{{hourTwoDigit}}</td></tr>
        <tr><td>iso</td><td>{{iso}}</td></tr>
        <tr><td>minute</td><td>{{minute}}</td></tr>
        <tr><td>minuteTwoDigit</td><td>{{minuteTwoDigit}}</td></tr>
        <tr><td>month</td><td>{{month}}</td></tr>
        <tr><td>monthName</td><td>{{monthName}}</td></tr>
        <tr><td>monthNameShort</td><td>{{monthNameShort}}</td></tr>
        <tr><td>monthTwoDigit</td><td>{{monthTwoDigit}}</td></tr>
        <tr><td>second</td><td>{{second}}</td></tr>
        <tr><td>secondTwoDigit</td><td>{{secondTwoDigit}}</td></tr>
        <tr><td>year</td><td>{{year}}</td></tr>
        <tr><td>yearTwoDigit</td><td>{{yearTwoDigit}}</td></tr>
      </tbody>
    </table>
  </template>
</amp-date-display>

区域设置

可以通过指定 locale 属性来配置国际化的月份和工作日名称。

例如,下面的 amp-date-display 组件以德语、法语、捷克语和英语显示相同的日期信息。

<amp-date-display datetime="now" locale="de" layout="fixed" width="360" height="20">
  <template type="amp-mustache">
    <div>de: {{dayName}} {{day}} {{monthName}} {{year}}</div>
  </template>
</amp-date-display>
<amp-date-display datetime="now" locale="fr" layout="fixed" width="360" height="20">
  <template type="amp-mustache">
    <div>fr: {{dayName}} {{day}} {{monthName}} {{year}}</div>
  </template>
</amp-date-display>
<amp-date-display datetime="now" locale="cs" layout="fixed" width="360" height="20">
  <template type="amp-mustache">
    <div>cs: {{dayName}} {{day}} {{monthName}} {{year}}</div>
  </template>
</amp-date-display>
<amp-date-display datetime="now" locale="en-GB" layout="fixed" width="360" height="20">
  <template type="amp-mustache">
    <div>en-GB: {{dayName}} {{day}} {{monthName}} {{year}}</div>
  </template>
</amp-date-display>
需要进一步解释?

如果本页上的解释没有涵盖您的所有问题,请随时与其他 AMP 用户联系,讨论您的确切用例。

转到 Stack Overflow
一个未解释的功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的长期参与者,但我们也欢迎您为自己特别关注的问题做出一次性贡献。

在 GitHub 上编辑示例