Vanilla DateTime Picker: Zero-Dependency JS Date-Time Input
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.
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.<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.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.
The post Vanilla DateTime Picker: Zero-Dependency JS Date-Time Input appeared first on CSS Script.
Apple recently released its newest budget smartphone - the Apple iPhone 17e - on March…
Blight: Survival has reemerged with a new gameplay trailer — and its developers are promising…
Bluetti is well known for its high quality yet affordable power stations and solar generators.…
There’s something endlessly endearing about a good-natured dummy. Just a happy, optimistic doofus that can…
(KTAB/KRBC) - The Sweetwater Rattlesnake Roundup Parade for 2026 is taking place at 4:30 p.m.,…
Editor’s Note: A Grand Jury indicted the following suspects on felony charges in Taylor County,…
This website uses cookies.