'Is there a way to display the form field as mm/yyyy in html form?
I'm trying to have a form field with mm/yyyy as an input. Can you help me in suggesting a way to have this date format ?
Solution 1:[1]
In your html code use the input element and give it the "month" type (YYYY-MM)
<input type="month" id="something" />
To get the date and year, you can use javascript
var date = document.getElementById("something").value
Solution 2:[2]
Try to use the below code in your form. It should allow you to enter the month first then the year after.
<label for="start">Date:</label>
<input type="month" id="start">
Solution 3:[3]
You can try this code to see.
<p id="time"></p>
<script>
const now = new Date();
const year = now.getFullYear();
let month = now.getMonth() + 1;
if (month < 10)
month = '0' + month;
const time = month + '/' + year;
document.getElementById("time").innerHTML = time;
</script>
Solution 4:[4]
You can try this pattern for your html input field like this:
<form>
<input type="text" pattern="[0-1]{1}[0-9]{1}/[0-9]{4}">
</form>
It will not accept anything unless it matches "MM/YYYY" pattern.
Solution 5:[5]
<label for="birthdayMonth">Enter Birthday month-year:</label> <input type="month" id="birthdayMonth" name="birthdayMonth">
Try this:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | TobyLoby |
Solution 2 | ouflak |
Solution 3 | Hello_World |
Solution 4 | Frida |
Solution 5 |