JSFiddle - React, Tailwind, and code Playground

by Patrick Robles

HTML

<a href="#" id="linkMonth">Click</a>
<br><br><br>
<select name="month" id="month">
    <option>Choose Month</option>
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
</select>
<select name="year" id="year"></select>

JavaScript

$('#linkMonth').click(function() {
    
    var min = new Date().getFullYear();
    var till =  2014;
    select = document.getElementById('year');

    for (var i = min; i>=till; i--){
        var opt = document.createElement('option');
        opt.value = i;
        opt.innerHTML = i;
        select.appendChild(opt);
    }
    
    var currentDate = new Date();
    var currentMonth=('0'+(currentDate.getMonth()+1)).slice(-2);
    var year = currentDate.getFullYear();
    
    var from = year +"/"+ currentMonth +"/01"; 
    var to = year +"/"+ currentMonth +"/" + daysInMonth(currentMonth, year); 
    
    alert(from +"-"+ to);
});

$('#month,#year').change(function() {

    var choice =  $('#month option:selected').text();
    if(choice != 'Choose Month'){
        
        var month =  $('#month option:selected').val();
        var year =  $('#year option:selected').val();
        
        var from = year +"/"+ month +"/01"; 
        var to = year +"/"+ month +"/" + daysInMonth(month, year); 
    
        alert(from +"-"+ to);
    }
});

function daysInMonth(month, year) {
    return new Date(year, month, 0).getDate();
}