How to get dynamic yyyymm dropdown field in jsp?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #16
    btw, months in Date go from 0 to 11.

    and you can decrement the month by using Date.setMonth()

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #17
      You need to embed a loop in your year loop to iterate the months.

      Comment

      • Sherin
        New Member
        • Jan 2020
        • 77

        #18
        I created a code which gives me yyyymm dynamically upto current year month..

        Code:
        function dropdown(monthfield, yearfield){
                       var monthtext=['01','02','03','04','05','06','07','08','09','10','11','12'];
                       var today=new Date()
                       var monthfield=document.getElementById(monthfield)
                       var yearfield=document.getElementById(yearfield)
                       var thisyear=today.getFullYear()
                       var thismonth=today.getMonth()
                       
                       var id = 0;
                       for (var y=0; y<=20; y++){
                           for (var m=(monthtext.length-1); m>=0; m--, id++){
                               if(m<=thismonth){
                                   yearfield.options[id]=new Option(thisyear+"-"+monthtext[m], thisyear+""+monthtext[m])
                               }else{}
                           }
                           thisyear-=1
                           thismonth = 12;
                       }
                       yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
                   }

        Comment

        Working...