Socket
Socket
Sign inDemoInstall

@ampproject/amp-date-countdown

Package Overview
Dependencies
5
Maintainers
16
Versions
95
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ampproject/amp-date-countdown

AMP HTML amp-date-countdown Component


Version published
Weekly downloads
2
Maintainers
16
Install size
339 kB
Created
Weekly downloads
 

Readme

Source

Bento Date Countdown

Usage

The Bento Date Countdown component displays a countdown sequence to a specified date. The Bento Date Countdown extension provides a list of time parameters. Refer to the returned time parameters section below for more information on the available time parameters.

Web Component

You must include each Bento component's required CSS library to guarantee proper loading and before adding custom styles. Or use the light-weight pre-upgrade styles available inline. See Layout and style.

The examples below demonstrate use of the <bento-date-countdown> web component.

Example: Import via npm

[example preview="top-frame" playground="false"]

Install via npm:

npm install @ampproject/bento-date-countdown
import '@ampproject/bento-date-countdown';

[/example]

Example: Include via <script>

The example below contains an bento-date-countdown with three sections. The expanded attribute on the third section expands it on page load.

[example preview="top-frame" playground="false"]

<head>
  <script src="https://cdn.ampproject.org/custom-elements-polyfill.js"></script>
  <script async src="https://cdn.ampproject.org/v0/bento-date-countdown-1.0.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-date-countdown-1.0.css">
</head>
<body>
  <bento-date-countdown timeleft-ms="200000000" biggest-unit="HOURS">
    <template type="amp-mustache">
      <div>
        <span>{{days}} {{dd}} {{d}}</span>
        <br />
        <span>{{hours}} {{hh}} {{h}}</span>
        <br />
        <span>{{minutes}} {{mm}} {{m}}</span>
        <br />
        <span>{{seconds}} {{ss}} {{s}}</span>
      </div>
    </template>
  </bento-date-countdown>
</body>

[/example]

Interactivity and API usage

The Bento Date Countdown component does not have an imperative API. However, the Bento Date Countdown Web Component will render a mustache-template, provided by the consumer in a <template> tag with type="amp-mustache". This template may interpolate date-related parameters. See the Returned Time Parameters section for a full list of all the available parameters.

Layout and style

Each Bento component has a small CSS library you must include to guarantee proper loading without content shifts. Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles.

<link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-date-countdown-1.0.css">

Alternatively, you may also make the light-weight pre-upgrade styles available inline:

<style data-bento-boilerplate>
  bento-date-countdown {
    display: block;
    overflow: hidden;
    position: relative;
  }
</style>
Attributes

To provide the datetime to countdown to, you must specify at least one of these required attributes: end-date, timeleft-ms, timestamp-ms, timestamp-seconds.

end-date

An ISO formatted date to count down to. For example, 2020-06-01T00:00:00+08:00.

Used to determine the datetime to countdown to.

timestamp-ms

A POSIX epoch value in milliseconds; assumed to be UTC timezone. For example, timestamp-ms="1521880470000".

Used to determine the datetime to countdown to.

timestamp-seconds

A POSIX epoch value in seconds; assumed to be UTC timezone. For example, timestamp-seconds="1521880470".

Used to determine the datetime to countdown to.

timeleft-ms

A value in milliseconds left to be counting down. For example, 48 hours left timeleft-ms="172800000".

Used to determine the datetime to countdown to.

offset-seconds (optional)

A positive or negative number that represents the number of seconds to add or subtract from the end-date. For example, offset-seconds="60" adds 60 seconds to the end-date.

when-ended (optional)

Specifies whether to stop the timer when it reaches 0 seconds. The value can be set to stop (default) to indicate the timer to stop at 0 seconds and will not pass the final date or continue to indicate the timer should continue after reaching 0 seconds.

locale (optional)

An internationalization language string for each timer unit. The default value is en (for English).

Supported values:

CodeLanguage
deGerman
enEnglish
esSpanish
frFrench
idIndonesian
itItalian
jaJapanese
koKorean
nlDutch
ptPortuguese
ruRussian
thThai
trTurkish
viVietnamese
zh-cnChinese Simplified
zh-twChinese Traditional
biggest-unit (optional)

Allows the bento-date-countdown component to calculate the time difference based on the specified biggest-unit value. For example, assume there are 50 days 10 hours left, if the biggest-unit is set to hours, the result displays 1210 hours left.

  • Supported values: days, hours, minutes, seconds
  • Default: days
count-up (optional)

Include this attribute to reverse the direction of the countdown to count up instead. This is useful to display the time elapsed since a target date in the past. To continue the countdown when the target date is in the past, be sure to include the when-ended attribute with the continue value. If the target date is in the future, bento-date-countdown will display a decrementing (toward 0) negative value.

Styling

You may use the bento-date-countdown element selector to style the Bento Date Countdown component freely.

Preact/React Component

The examples below demonstrates use of the <BentoDateCountdown> as a functional component usable with the Preact or React libraries.

Example: Import via npm

[example preview="top-frame" playground="false"]

Install via npm:

npm install @ampproject/bento-date-countdown
import React from 'react';
import { BentoDateCountdown } from '@ampproject/bento-date-countdown/react';
import '@ampproject/bento-date-countdown/styles.css';

function App() {
  return (
    <BentoDateCountdown
      datetime={200000000}
      biggestUnit={'HOURS'}
      render={(data) => (
        <div>
          <span>{`${data.days} ${data.dd} ${data.d}`}</span>
          <br />
          <span>{`${data.hours} ${data.hh} ${data.h}`}</span>
          <br />
          <span>{`${data.minutes} ${data.mm} ${data.m}`}</span>
          <br />
          <span>{`${data.seconds} ${data.ss} ${data.s}`}</span>
        </div>
      )}
    />
  );
}

[/example]

Interactivity and API usage

The Bento Date Countdown component does not have an imperative API. However, the Bento Date Countdown Preact/React component does accept a render prop that renders the consumer's template. This render prop should be a function which the Bento Date Countdown Preact/React component can use to render its template. The render callback will be provided a variety of date-related parameters for consumers to interpolate in the rendered template. See the render prop section for more information.

Layout and style

The Bento Date Countdown Preact/React component allows consumers to render their own templates. These templates may use inline styles, <style> tags, Preact/React components that import their own stylesheets.

Props
datetime

Required prop. Denotes the date and time as a Date, String, or Nuumber. If String, must be a standard ISO 8601 date string (e.g. 2017-08-02T15:05:05.000Z) or the string now. If set to now, it will use the time the page loaded to render its template. If Number, must be a POSIX epoch value in milliseconds.

locale

An internationalization language string for each timer unit. The default value is en (for English). This prop supports all values that are supported by the user's browser.

whenEnded

Specifies whether to stop the timer when it reaches 0 seconds. The value can be set to stop (default) to indicate the timer to stop at 0 seconds and will not pass the final date or continue to indicate the timer should continue after reaching 0 seconds.

biggestUnit

Allows the bento-date-countdown component to calculate the time difference based on the specified biggest-unit value. For example, assume there are 50 days 10 hours left, if the biggest-unit is set to hours, the result displays 1210 hours left.

  • Supported values: days, hours, minutes, seconds
  • Default: days
countUp

Include this prop to reverse the direction of the countdown to count up instead. This is useful to display the time elapsed since a target date in the past. To continue the countdown when the target date is in the past, be sure to include the when-ended prop with the continue value. If the target date is in the future, bento-date-countdown will display a decrementing (toward 0) negative value.

render

Optional callback that should render a template. The callback will be provided an object with properties/values related to the date expressed in datetime. By default, the Bento Date Countdown component will display the localeString form of the Date for the given locale and localeOption. See the Returned Time Parameters section for more details on how each property will be displayed.

(dateParams: DateParams) => JSXInternal.Element
interface DateParams {
  day: number;
  dayName: string;
  dayNameShort: string;
  dayPeriod: string;
  dayTwoDigit: string;
  hour: number;
  hour12: number;
  hour12TwoDigit: string;
  hourTwoDigit: string;
  iso: string;
  localeString: string;
  minute: number;
  minuteTwoDigit: string;
  month: number;
  monthName: string;
  monthNameShort: string;
  monthTwoDigit: string;
  second: number;
  secondTwoDigit: string;
  timeZoneName: string;
  timeZoneNameShort: string;
  year: number;
  yearTwoDi: string;
}

Returned Time Parameters

This table lists the format you can specify in your Mustache template:

FormatMeaning
dday - 0, 1, 2,...12, 13..Infinity
ddday - 00, 01, 02, 03..Infinity
hhour - 0, 1, 2,...12, 13..Infinity
hhhour - 01, 02, 03..Infinity
mminute - 0, 1, 2,...12, 13..Infinity
mmminute - 01, 01, 02, 03..Infinity
ssecond - 0, 1, 2,...12, 13..Infinity
sssecond - 00, 01, 02, 03..Infinity
daysinternationalization string for day or days
hoursinternationalization string for hour or hours
minutesinternationalization string for minute or minutes
secondsinternationalization string for second or seconds
Samples of formatted values

This table provides examples of formatted values specified in a Mustache template, and a sample of what the output:

FormatSample OutputRemarks
{hh}:{mm}:{ss}04:24:06-
{h} {hours} and {m} {minutes} and {s} {seconds}4 hours and 1 minutes and 45 seconds-
{d} {days} {h}:{mm}1 day 5:03-
{d} {days} {h} {hours} {m} {minutes}50 days 5 hours 10 minutes-
{d} {days} {h} {hours} {m} {minutes}20 days 5 hours 10 minutes-
{h} {hours} {m} {minutes}240 hours 10 minutesbiggest-unit='hours'
{d} {days} {h} {hours} {m} {minutes}50 天 5 小时 10 分钟locale='zh-cn'

FAQs

Last updated on 14 Oct 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc