eonasdan/bootstrap-datetimepicker
A Bootstrap-based date and time picker for jQuery and Moment.js, offering flexible formatting, localization, min/max dates, disabled dates, and keyboard/mouse controls. Easy to integrate with Bootstrap forms and supports multiple picker views and options.
Install via CDN (Recommended for quick use)
Add to your resources/views/layouts/app.blade.php (or equivalent):
<!-- CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/eonasdan-bootstrap-datetimepicker/5.0.0/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
<!-- JS (after jQuery and Bootstrap) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/eonasdan-bootstrap-datetimepicker/5.0.0/js/bootstrap-datetimepicker.min.js"></script>
Basic Initialization In your Blade template:
<input type="text" class="form-control datetimepicker" id="example-datetimepicker">
<script>
$(function() {
$('#example-datetimepicker').datetimepicker();
});
</script>
First Use Case: Form Input Use with Laravel Form Requests:
// app/Http/Requests/StoreEventRequest.php
public function rules() {
return [
'event_date' => 'required|date_format:Y-m-d H:i',
];
}
Bind to a model:
$event->event_date = $request->input('event_date');
Dynamic Initialization Use Laravel Blade directives to conditionally initialize:
@if($showDatepicker)
<script>
$(function() {
$('#datetimepicker').datetimepicker({
useCurrent: false,
format: 'YYYY-MM-DD HH:mm'
});
});
</script>
@endif
Integration with Livewire/Alpine Livewire Example:
// app/Http/Livewire/EventForm.php
public function mount() {
$this->event_date = now()->format('Y-m-d H:i');
}
public function render() {
return view('livewire.event-form');
}
<!-- resources/views/livewire/event-form.blade.php -->
<input type="text" wire:model="event_date" id="datetimepicker">
<script>
document.addEventListener('livewire:init', () => {
$('#datetimepicker').datetimepicker();
});
</script>
Reusable Component Create a Blade component:
php artisan make:component DateTimePicker
<!-- resources/views/components/date-time-picker.blade.php -->
<input type="text" class="form-control" id="datetimepicker-{{ $id }}"
wire:model="{{ $model ?? null }}" {{ $attributes }}>
<script>
$(function() {
$('#datetimepicker-{{ $id }}').datetimepicker({
@if($options)
{{ json_encode($options) }}
@endif
});
});
</script>
Validation Feedback Combine with Laravel validation messages:
<x-date-time-picker wire:model="event_date" id="event-date" />
@error('event_date')
<div class="text-danger">{{ $message }}</div>
@enderror
Localization: Use moment.js locales for non-English dates:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/locale/fr.js"></script>
<script>
$(function() {
$('#datetimepicker').datetimepicker({
locale: 'fr'
});
});
</script>
Server-Side Timezones: Convert user input to UTC in your model:
// app/Models/Event.php
protected $casts = [
'event_date' => 'datetime',
];
// Automatically converts to UTC on save
AJAX Forms: Ensure datetimepicker updates hidden inputs:
<input type="text" id="datetimepicker" class="form-control">
<input type="hidden" name="event_date" id="event_date_hidden">
<script>
$('#datetimepicker').on('dp.change', function(e) {
$('#event_date_hidden').val(e.date.format('YYYY-MM-DD HH:mm'));
});
</script>
jQuery Dependency
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/eonasdan-bootstrap-datetimepicker/5.0.0/js/bootstrap-datetimepicker.min.js"></script>
Timezone Mismatches
2023-12-31 23:00 in their local timezone, but the server stores it as UTC 2024-01-01 04:00.$userInput = '2023-12-31 23:00';
$eventDate = Carbon\Carbon::parse($userInput)->timezone('UTC');
Mobile Compatibility
touchstart event listeners or use a mobile-friendly alternative like Pikaday.Conflicting Selectors
<input type="text" data-datetimepicker="event-date" class="datetimepicker">
$('[data-datetimepicker]').datetimepicker();
Deprecated Methods
$.fn.datetimepicker.defaults).// Old (deprecated)
$.fn.datetimepicker.defaults.timeFormat = 'HH:mm';
// New
$('#datetimepicker').datetimepicker({
timepicker: true,
format: 'YYYY-MM-DD HH:mm'
});
Check Console for Errors
F12) and look for Uncaught TypeError or Cannot read property 'datetimepicker' of undefined.Verify Initialization Timing
$(document).ready(function() {
// Initialize here
});
Inspect Hidden Inputs
console.log() to verify the hidden input value matches the displayed date:
$('#datetimepicker').on('dp.change', function(e) {
console.log('Selected date:', e.date.format());
console.log('Hidden input:', $('#event_date_hidden').val());
});
Disable for Testing
// In your initialization
$('#datetimepicker').data('DateTimePicker').destroy();
Custom Formatting
$.fn.datetimepicker.dates['en'].format = 'DD/MM/YYYY HH:mm';
Event Listeners
$('#datetimepicker').on('dp.show', function() {
console.log('Picker shown');
}).on('dp.hide', function() {
console.log('Picker hidden');
});
Dynamic Updates
$.ajax({
url: '/update-date',
success: function() {
$('#datetimepicker').data('DateTimePicker').update();
}
});
Theming
.datetimepicker {
z-index: 9999 !important;
}
.datetimepicker table tr td.active {
background-color: #3498db !important;
}
useCurrent vs defaultDate
useCurrent: true sets the current date/time on initialization.defaultDate allows setting a specific default (e.g., new Date(2023, 0, 1)).sideBySide vs widgetPositioning
sideBySide: true places date/time pickers side-by-side (deprecated in v5).widgetPositioning: { horizontal: 'auto' } for modernHow can I help you explore Laravel packages today?