I am trying to format my HTML5 input date type date format to "01-Jan-2024". I have tried placeholder="dd-mm-yyyy" value=""
and required pattern="[0-9]{2}-[0-9]{3}-[0-9]{4}"
. Nothing working. Can you please help me how to format the date to "d-MM-yyyy" (like '01-Jan-2024')
<div class="form-group col-md-4">
<label asp-for="OrderDate"></label>
<div class="input-group">
<input type="date" asp-for="OrderDate" class="form-control">
</div>
<span asp-validation-for="OrderDate" class="text-danger"></span>
</div>
4
Answers
The HTML5 input date type only supports the format (yyyy-MM-dd) and does not support the format you want (d-MM-yyyy).
You can still achieve the desired format using JavaScript.
With this script, whenever a date is selected, it will be reformatted to the desired format before being displayed in the input field.
You could implement it like this:
Certainly! HTML5 input type date doesn’t support custom formats directly. However, you can achieve the desired format using JavaScript. Here’s a step-by-step solution:
Here’s how you can do it:
`
This code listens for changes in the input date field. When a date is selected, it formats it using the
formatDate()
function and displays the formatted date in the "formattedDate" input field.This way, you can achieve the desired date format of "01-Jan-2024".