Categories: CSSScriptWeb Design

Vanilla DateTime Picker: Zero-Dependency JS Date-Time Input

Vanilla DateTime Picker is a modern, clean, zero-dependency, fully customizable JavaScript DateTime picker that attaches to standard text inputs.

Features:

  • Multiple Build Formats: Includes ESM modules for modern bundlers and IIFE builds for direct browser usage.
  • Flexible Time Selection: Configurable minute steps (1, 2, 5, 10, 15, or 30 minutes) and customizable hour ranges.
  • Keyboard Navigation: Full keyboard support with arrow keys, Tab navigation, and shortcut keys for common actions.
  • Sponsored
  • Date Range Constraints: Supports minimum and maximum date boundaries to restrict selectable dates.
  • Custom Formatting: Provides customizable format and parse functions for different date-time display patterns.
  • Accessibility: Implements ARIA attributes and follows grid navigation patterns for screen reader compatibility.
  • Programmatic Control: An API is available to open, close, set, and clear dates from your own scripts.

How to use it:

1. Link the stylesheet in your HTML’s <head>.

<link rel="stylesheet" href="/dist/vanilla-js-datetime-picker.css"/>

2. Load the JavaScript. The library supports both ESM imports for bundled applications and direct script inclusion for traditional setups.

<script type="module">
  import DateTimePicker from './dist/vanilla-js-datetime-picker.esm.js';
</script>
<script src="path/to/vanilla-js-datetime-picker.iife.min.js"></script>

3. Add a standard text input to your HTML.

<input id="example" type="text" placeholder="YYYY-MM-DD HH:mm"/>

4. Create a new DateTimePicker instance and pass the selector for your input.

const picker = new DateTimePicker('#example', {
  // options here
});

5. Pass a configuration object as the second argument to customize the picker. All available options:

  • minuteStep: Sets the step interval for the minute selector. Valid values are 1, 2, 5, 10, 15, and 30.
  • startOfWeek: Defines the first day of the week, where 0 is Sunday and 6 is Saturday.
  • defaultHour: The hour selected by default when no time is set.
  • defaultMinute: The minute selected by default when no time is set.
  • format: A function that receives a Date object and should return a formatted string for the input field.
  • parse: A function that receives the input string and should return a Date object or null if the string is invalid.
  • min: The minimum selectable date. Accepts a Date object or a YYYY-MM-DD string.
  • max: The maximum selectable date. Accepts a Date object or a YYYY-MM-DD string.
  • zIndex: The CSS z-index for the picker popover. Defaults to 9999.
  • initial: A Date object to pre-fill the input with if it’s initially empty.
  • highlightToday: When true, adds a special class to the current day in the calendar.
  • showTimeHeaders: When true, displays the “Hours” and “Minutes” labels above the time selection columns.
  • hourLabel: Custom text for the “Hours” label.
  • minuteLabel: Custom text for the “Minutes” label.
  • hourMin: The minimum hour (0-23) to display in the hours list.
  • hourMax: The maximum hour (0-23) to display in the hours list.
  • extendHourBoundsWithCurrent: If true, the currently set hour will be visible in the list even if it’s outside the hourMin and hourMax range.
const picker = new DateTimePicker('#example', {
  minuteStep: 5,
  startOfWeek: 0, 
  defaultHour: 9,
  defaultMinute: 0,
  format: (date) => {
    const p = (n) => String(n).padStart(2, '0');
    return `${date.getFullYear()}-${p(date.getMonth() + 1)}-${p(date.getDate())} ${p(date.getHours())}:${p(date.getMinutes())}`;
  },
  parse: (str) => {
    if (!str) {
      return null;
    }
    const d = new Date(str);
    if (!isNaN(d)) {
      return d;
    }
    const m = str.match(/^s*(d{4})-(d{2})-(d{2})[ T](d{2}):(d{2})(?::(d{2}))?s*$/);
    if (m) {
      const [_, y, mo, dd, hh, mi, ss] = m;
      const dt = new Date(+y, +mo - 1, +dd, +hh, +mi, +(ss || 0), 0);
      if (!isNaN(dt)) {
        return dt;
      }
    }
    return null;
  },
  min: null,
  max: null,
  zIndex: 9999,
  initial: new Date(),
  highlightToday: true,
  showTimeHeaders: true,
  hourLabel: 'Hours',
  minuteLabel: 'Minutes',
  hourMin: 0,
  hourMax: 23,
  extendHourBoundsWithCurrent: true,
});

6. API methods to control the DateTime picker programmatically.

Sponsored
  • picker.open(): Manually opens the picker UI.
  • picker.close(): Closes the picker.
  • picker.getDate(): Returns the currently selected Date object or null.
  • picker.setDate(new Date()): Sets the picker’s selected date.
  • picker.setInitial(new Date()): Sets the baseline date used by the reset button.
  • picker.getInitial(): Returns the initial baseline Date object or null.
  • picker.clear(): Clears the selected date and the input’s value.
  • picker.destroy(): Removes the picker and all associated event listeners.

Alternatives:

  • flatpickr: This is a very popular and powerful alternative. It’s also lightweight and dependency-free, but it comes with a much larger feature set, including date range selection, more plugins, and extensive theming.
  • Tempus Dominus: Built as a successor to Bootstrap Datetimepicker, this library integrates tightly with Bootstrap styling conventions. It offers icon customization, linked pickers for date ranges, and validation feedback through Bootstrap’s form states.
  • Native <input type="datetime-local">: Don’t forget the browser’s native element. It’s the most lightweight option of all, but its appearance and user experience are inconsistent across different browsers, and styling options are very limited.

FAQs:

Q: How does the library handle timezone conversions?
A: The picker works entirely in the browser’s local timezone. It creates Date objects using the local timezone offset and expects the format and parse functions to handle timezone display if needed. For applications requiring UTC or specific timezone handling, implement custom format and parse functions that convert between local time and your target timezone.

Q: Can I style the picker to match my application’s design system?
A: The external CSS file uses classes prefixed with .vdtp- for all styling. Override these classes in your application’s stylesheet to customize colors, spacing, typography, and layout. The picker’s DOM structure remains consistent, making it straightforward to target specific elements. The z-index option controls popover stacking order relative to your page content. For Content Security Policy compliance, avoid using the CSS-injecting variant and rely on the external stylesheet.

Q: What happens when users type directly into the input field?
A: The picker attaches an input event listener that parses the field value using the configured parse function. If parsing succeeds, the library updates its internal state to match the typed value. The picker synchronizes the calendar view and time selection to reflect the parsed date. If parsing fails, the library maintains the previous valid state. Users can type dates in any format the parse function recognizes, providing flexibility beyond picker interaction.

Q: How do I restrict time selection to specific hours or intervals?
A: Use the hourMin and hourMax options to limit the hour list range. Set minuteStep to control time slot granularity. For more complex restrictions, implement validation in the format function or add an event listener to the input field. The extendHourBoundsWithCurrent option allows the currently selected hour to display even when outside the configured range, preventing users from becoming locked out of their selection.

Related Resources:

The post Vanilla DateTime Picker: Zero-Dependency JS Date-Time Input appeared first on CSS Script.

rssfeeds-admin

Share
Published by
rssfeeds-admin

Recent Posts

The Total Wireless by Verizon “Apple iPhone 17e On Us” Deal Explained (New Release)

Apple recently released its newest budget smartphone - the Apple iPhone 17e - on March…

16 minutes ago

Blight: Survival Remerges After 1.5 Million Steam Wishlists and a Viral Trailer With a New Look at Gameplay

Blight: Survival has reemerged with a new gameplay trailer — and its developers are promising…

17 minutes ago

The Bluetti AC70 768Wh 1,000W LiFePO4 Power Station Is 20% Cheaper on AliExpress Than on Amazon

Bluetti is well known for its high quality yet affordable power stations and solar generators.…

1 hour ago

Stupid Never Dies Preview: An Outrageous Action RPG with Heart (Even if that Heart Isn’t Beating)

There’s something endlessly endearing about a good-natured dummy. Just a happy, optimistic doofus that can…

1 hour ago

WATCH LIVE: Sweetwater Rattlesnake Roundup Parade

(KTAB/KRBC) - The Sweetwater Rattlesnake Roundup Parade for 2026 is taking place at 4:30 p.m.,…

2 hours ago

Grand Jury: Drug cases make up most of Taylor County indictments this week

Editor’s Note: A Grand Jury indicted the following suspects on felony charges in Taylor County,…

2 hours ago

This website uses cookies.