Is there a way to display the form field as mm/yyyy in html form?

746 Views Asked by At

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 ?

5

There are 5 best solutions below

0
TobyLoby On BEST ANSWER

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
1
Vishwa On

<label for="birthdayMonth">Enter Birthday month-year:</label> <input type="month" id="birthdayMonth" name="birthdayMonth"> Try this:

0
Sunny On

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">
1
Hello_World On

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>
0
Frida On

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.