Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

My issue is that I have a month picker in html, that is

HTML
<input type="month" id="mymonth">


and a textbox that is

HTML
<input type="text" id="mymonth2">


I want to select a month from month picker and set the value to mymonth2 as 2021-April for example but I get it as 2021-04.

Please help.

What I have tried:

JavaScript
$("#mymonth").change(function(){
      var mymonth = $(this).val();
      $("#mymonth2").val(mymonth);
    });
Posted

1 solution

you can use the following code:
const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

formatDate = function(d) {
	return d.getFullYear()+"-"+monthNames[d.getMonth()]
}


$("#mymonth").change(function(){
      var mymonth = $(this).val();
      $("#mymonth2").val(formatDate(mymonth));
});
 
Share this answer
 
Comments
nyt1972 16-Jan-21 8:24am    
Thanks a lot, just modified a bit
formatDate = function(d) {
var dd = new Date(d);
return dd.getFullYear()+"-"+monthNames[dd.getMonth()]
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900