bsTimeDuration is a jQuery period enter plugin for Bootstrap 5 that turns period akin to 2h 45m into integer minute values.
It accepts weeks, days, decimal hours, and clock-style durations, with configurable workday lengths for timesheets and mission estimates.
Options
- Settle for unit expressions, decimal hours, clock notation, and uncooked minute values.
- Preserve readable enter textual content separate from the named type worth.
- Protect Bootstrap enter sizes, courses, and related labels.
- Work with floating labels and enter teams.
- Format legitimate durations into unit-based textual content on blur.
- Mark invalid entries and emit validation occasions.
- Configure working-day and working-week lengths.
- Learn, update, clear, and restore period fields programmatically.
Use Circumstances
- Timesheet types report billable effort as minutes for payroll or invoicing.
- Venture planners settle for job estimates written in weeks, days, and hours.
- A support-ticket editor captures decision time via quick textual content entries.
- Service-window types measure elapsed time in mounted 24-hour day items.
How To Use It
Set up and Load the Required Recordsdata
Obtain the plugin and replica dist/bs-time-duration.min.js into your mission’s js listing. Load jQuery 3 or later earlier than the plugin. Bootstrap 5 CSS is required for the shape look.
Primary Utilization
<!-- Bootstrap 5 CSS is non-obligatory for plugin habits. -->
<hyperlink rel="stylesheet" href="css/bootstrap.min.css">
<type id="estimate-form" technique="get">
<label for="estimated-effort" class="form-label">Estimated effort</label>
<enter
sort="textual content"
id="estimated-effort"
identify="estimatedMinutes"
class="form-control"
placeholder="Attempt 2h 45m"
autocomplete="off">
<button sort="submit" class="btn btn-primary mt-2">Save</button>
</type>
<!-- At all times load jQuery earlier than the plugin file. -->
<script src="js/jquery.min.js"></script>
<script src="js/bs-time-duration.min.js"></script>
<script>
$(operate () {
$('enter[name="estimatedMinutes"]').bsTimeDuration();
});
</script>
Accepted Period Codecs
| Enter | Outcome |
|---|---|
2h 45m |
165 minutes |
1w 2nd |
3,360 minutes with the default workday settings |
1.5 or 1,5 |
90 minutes |
1,25h |
75 minutes |
2:30 |
150 minutes |
90 |
90 minutes |
1h30m |
90 minutes |
Superior Examples
These examples use the jQuery and plugin recordsdata loaded in Primary Utilization. Embody Bootstrap CSS for the styled fields.
1. Validate a Required Timesheet Entry Earlier than Submission
A timesheet can test the precise textual content earlier than it processes the minute worth. Native HTML required validation solely catches an empty seen area; this submit handler checks malformed nonempty durations too.
<type id="work-log-form">
<label for="logged-time" class="form-label">Hours labored</label>
<enter id="logged-time" identify="workedMinutes" sort="textual content"
class="form-control" placeholder="3h 20m" required>
<button class="btn btn-primary mt-2" sort="submit">Report time</button>
<p id="work-log-message" function="standing" aria-live="well mannered"></p>
</type>
<script>
$(operate () {
// Preserve a reference to the unique enter, which turns into hidden.
var $labored = $('enter[name="workedMinutes"]').bsTimeDuration({
required: true
});
$('#work-log-form').on('submit', operate (occasion) {
occasion.preventDefault(); // Preview submitted values on this instance.
var textual content = $('#logged-time').val(); // The ID belongs to the seen enter.
var outcome = $labored.bsTimeDuration('parse', textual content);
if (!outcome.legitimate || outcome.minutes === null) {
$('#work-log-message').textual content('Enter a legitimate work period.');
return;
}
// The named hidden area now holds the integer minute worth.
$('#work-log-message').textual content('Kind knowledge: ' + $(this).serialize());
});
});
</script>
2. Enter Calendar-Day Durations in a Bootstrap Enter Group
A service window measured in mounted 24-hour blocks makes use of completely different items from an ordinary workweek. Set at some point to 1,440 minutes and one week to seven days.
<label for="delivery-window" class="form-label">Supply window</label>
<div class="input-group">
<span class="input-group-text">Period</span>
<enter id="delivery-window" identify="deliveryWindowMinutes" sort="textual content"
class="form-control" placeholder="1d 4h">
<span class="input-group-text">w / d / h / m</span>
</div>
<script>
$(operate () {
$('enter[name="deliveryWindowMinutes"]').bsTimeDuration({
minutesPerDay: 1440, // 24 hours per day
daysPerWeek: 7 // Seven-day week
});
});
</script>
3. Load a Saved Estimate and Replace a Stay Abstract
An edit display can fill a period area from saved minute knowledge and refresh its abstract at any time when the submitted worth modifications.
<label for="ticket-effort" class="form-label">Ticket effort</label>
<enter id="ticket-effort" identify="ticketEffortMinutes" sort="textual content"
class="form-control" placeholder="Enter an estimate">
<button id="load-ticket-effort" sort="button">Load saved estimate</button>
<button id="clear-ticket-effort" sort="button">Clear</button>
<p id="ticket-effort-summary" function="standing" aria-live="well mannered">No estimate</p>
<script>
$(operate () {
var $effort = $('enter[name="ticketEffortMinutes"]');
// Bind to the unique named component, earlier than initialization.
$effort.on('change.bs.timeDuration', operate (occasion, minutes, formatted) {
$('#ticket-effort-summary').textual content(
minutes === null ? 'No estimate' : formatted + ' (' + minutes + ' min)'
);
});
$effort.bsTimeDuration();
$('#load-ticket-effort').on('click on', operate () {
// Simulate a worth retrieved from an present report.
$effort.bsTimeDuration('minutes', 195);
});
$('#clear-ticket-effort').on('click on', operate () {
$effort.bsTimeDuration('minutes', null);
});
});
</script>
Configuration Choices
minutesPerDay(quantity, default480): Units the minutes represented by1d; the worth have to be a optimistic secure integer.daysPerWeek(quantity, default5): Units the variety of days represented by1w; the worth have to be a optimistic secure integer.allowDays(boolean, defaulttrue): Accepts day items in person entries and makes use of them in formatted output.allowWeeks(boolean, defaulttrue): Accepts week items and makes use of them in formatted output.required(boolean, defaultfalse): Marks an empty entry invalid throughout plugin parsing.normalizeOnBlur(boolean, defaulttrue): Rewrites legitimate entries into unit-based textual content when focus leaves the sector.validateOnInput(boolean, defaulttrue): Updates the parsed worth and invalid state throughout enter occasions; setting it tofalsedefers synchronization till change or blur.decimalSeparator(string, default'.'): Seems within the 1.0.0 supply defaults, however the parser doesn’t learn this feature. Each comma and interval decimal enter work no matter its worth.invalidClass(string, default'is-invalid'): Selects the CSS class utilized to invalid seen entries.
API Strategies
// Initialize the unique named enter and hold its reference.
var $period = $('enter[name="estimatedMinutes"]').bsTimeDuration();
// Parse a string; returns { legitimate: true, minutes: 135 }.
$period.bsTimeDuration('parse', '2h 15m');
// Format an integer minute rely; returns '2h 15m'.
$period.bsTimeDuration('format', 135);
// Get the present numeric minute worth, or null for an empty/invalid entry.
$period.bsTimeDuration('minutes');
// Set the present period utilizing an integer variety of minutes.
$period.bsTimeDuration('minutes', 135);
// Clear the enter via the numeric setter.
$period.bsTimeDuration('minutes', null);
// Set the period via the string-based technique.
$period.bsTimeDuration('worth', '2h 15m');
// Learn the submitted hidden-field string, akin to '135'.
$period.bsTimeDuration('worth');
// Use the documented worth alias with a quantity or a period string.
$period.bsTimeDuration('val', 135);
$period.bsTimeDuration('val', '2h 15m');
// Learn the hidden-field string via the alias.
$period.bsTimeDuration('val');
// Take away the generated enter and restore the unique enter.
$period.bsTimeDuration('destroy');
The numeric minutes(worth) setter codecs its show instantly. worth(worth) and val(worth) settle for parseable strings, numbers, or null; numeric enter stays unformatted till blur. Calling a plugin technique earlier than initialization throws an error.
Occasions
var $period = $('enter[name="estimatedMinutes"]');
// Runs when the submitted worth modifications, together with when it turns into empty.
$period.on('change.bs.timeDuration', operate (occasion, minutes, formatted) {
console.log(minutes, formatted);
});
// Runs when the plugin encounters an invalid worth.
$period.on('invalid.bs.timeDuration', operate (occasion, rawValue) {
console.log('Invalid:', rawValue);
});
// Runs for a trusted person enter occasion, even when the textual content is invalid.
$period.on('userChange.bs.timeDuration', operate (occasion, rawValue, minutes, formatted, legitimate) {
console.log(rawValue, minutes, formatted, legitimate);
});
// Runs for every plugin occasion above; eventName is its full occasion identify.
$period.on('all.bs.timeDuration', operate (occasion, eventName) {
console.log('Plugin occasion:', eventName, Array.prototype.slice.name(arguments, 2));
});
// Initialize after attaching the occasion listeners.
$period.bsTimeDuration();
Alternate options and Associated Assets
FAQs
Q: Why is the period lacking from submitted type knowledge?
A: Add a identify attribute to the unique enter earlier than initialization. The plugin retains that identify on the hidden component that carries the minute worth.
Q: Why does the shape submit handler not run for an empty period?
A: The HTML required attribute on the seen enter can block the submit occasion via native constraint validation. The plugin’s required: true possibility solely modifications its personal parser validation. Use novalidate on the shape if JavaScript must deal with each validation error.
Q: Why does initializing the identical CSS class twice create one other enter?
A: The generated textual content area inherits the unique courses, and the hidden unique loses them. Repeating $('.js-duration').bsTimeDuration() selects the generated area. Preserve the unique jQuery reference or choose uninitialized inputs by their identify and visual enter sort.
Q: Can clock-style period entries exceed 24 hours?
A: Sure. An entry akin to 27:15 represents 1,635 minutes. The minutes portion should keep between 00 and 59.
Q: How do I modify the working-day configuration after initialization?
A: The plugin ignores a second initialization name on the unique enter. Save the present minute rely, name destroy on the unique jQuery reference, reinitialize with new choices, and restore the minutes if the period should keep unchanged.
This superior jQuery plugin is developed by ThomasDev-de. For extra Superior Usages, please test the demo web page or go to the official web site.

