Skip to content

About

⌚ A lightweight, customizable, Timepicker.

Topics

Resources

Stars

53 stars

Watchers

6 watching

Forks

Repository files navigation

TimePicker.js

NPM Version NPM Downloads npm bundle size GitHub Actions Workflow Status GitHub Release Date

A lightweight, customizable, TimePicker.

Timepicker anim

Demo

JSFiddle StackBlitz

How to use it?

→ NPM
npm i timepicker.js

Installing with npm needs Node.js 22, 24 or 26 (the LTS lines) and npm 10 or higher.

→ CDN Hosted - jsDelivr

Load:

<script src="https://cdn.jsdelivr.net/npm/timepicker.js/dist/timepicker.iife.js"></script>
Instantiate with some options and listen to changes
const timepicker = new TimePicker('#click-trigger', {
    theme: 'dark',
    lang: 'pt',
})
timepicker.on('change', (evt) => {
    console.log(evt)

    // {
    //     hour: string;
    //     minute: string;
    //     element: HTMLElement;
    // }
})

API

Constructor

new TimePicker(target, options)

target can be:

{String|Element} String or DOM node.

A picker binds to the element that exists when it is created. To use one with an input you add later, create the picker after the input is in the DOM.

Replacing that input binds nothing new. container.innerHTML += '…' does replace it, because it rebuilds every child of container. The picker stays attached to the old, detached element, and the new input won't open it. Add markup with container.insertAdjacentHTML('beforeend', '…') or container.append(…) instead, which keeps the existing inputs. Or create a new picker for the new element, after calling destroy() on the old one.

options is an object with the following possible properties:
  • theme: 'dark'; Theme color
  • lang: 'en'; Header language

Methods

timepicker.show()

To be used programatically. Same for hide().

All targets passed to the constructor will be shown.

timepicker.hide()

timepicker.destroy()

Removes the picker: its container, its listeners on the target, the document and its own elements, and every on() handler. After destroy(), show(), hide() and on() do nothing, and calling destroy() again is a no-op.

Events

timepicker.on('open', function (evt) {});

timepicker.on('close', function (evt) {});

timepicker.on('change', function (evt) {});

change receives { hour, minute, element }. hour and minute are strings such as '9' and '05', and each stays null until it has been picked, so the first change after picking only an hour has minute: null.

Themes

  • dark
  • red
  • pink
  • purple
  • deep-purple
  • indigo
  • blue
  • light-blue
  • cyan
  • teal
  • green
  • light-green
  • lime
  • yellow
  • amber
  • orange
  • deep-orange
  • brown
  • blue-grey

Sponsor

If timepicker.js is useful to you, you can sponsor its maintenance on GitHub.

About

⌚ A lightweight, customizable, Timepicker.

Topics

Resources

Stars

53 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages