I have an input where using bootstrap datepicker, but i want the output format to be like (Day – Month #, year)
ex: Wednesday – January 1, 2025.
<div class="col-md-6">
<div class="form-group">
<label>Date</label>
<div class="date input-group">
<input
type="text"
class="datepicker form-control"
id="date-input"
/>
</div>
</div>
</div>
here is my script for updating the input value
$(document).ready(function () {
$("#date-input").datepicker({
format: "mm/dd/yyyy",
autoclose: true,
});
$(".datepicker").on("changeDate", function (e) {
const selectedDate = e.date; // Get selected date as JavaScript Date object
const options = { weekday: "long", year: "numeric", month: "long", day: "numeric" };
const formattedDate = selectedDate.toLocaleDateString("en-US", options);
$("#date-input").datepicker("update", formattedDate);
});
});
but it does not update the input value. but it shows a correct output on my console.log
4
Answers
You gave to datepicker the
fomat: "mm/dd/yyyy"
.If you want (Day – Month #, year), just change it following the documentation.
https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#format.
fomat: "DD - MM d, yyyy"
You can use
toDisplay
function, to achieve [custom] format what you need.In your
changeDate
function you need to add the formatted date in the input the update function doesn’t set the input valueThis will solve your issue
Replace
The datepicker("update") method expects a date object or formatted date string compatible with the datepicker’s format
.val() directly sets the input’s value to the formatted date string
If you want to maintain the underlying date value for form submission while showing the formatted text, you can use a hidden input