468,490 Members | 2,598 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

How to get dynamic yyyymm dropdown field in jsp?

24
Hi,
I have two fields of date in this format yyyymm(from date and to date)
I have the following code:
Expand|Select|Wrap|Line Numbers
  1. function populateYearSelect() 
  2.             { 
  3.                 d = new Date(); 
  4.                 curr_year = d.getFullYear(); 
  5.                // curr_month = d.getMonth();
  6.  
  7.                 for(i = 0; i < 2; i++) 
  8.                 { 
  9.                     document.getElementById('year').options[i] = new Option(curr_year-i,curr_year-i); 
  10.  
  11.                    // document.month.option[curr_month].selected=true;
  12.  
  13.                 } 
  14.  
  15.             } 
This gives me dynamic year(upto current year)..
But as soon as I add the commented code
Expand|Select|Wrap|Line Numbers
  1. // curr_month = d.getMonth();
  2.  // document.month.option[curr_month].selected=true;
It fails...
I need date as follows:
If year is 2012 & month is june then list should show 201206

Now current month is june so it should show...201306,201305,201304,.......,201212,201211, 201210,......& so on..

Can any one help?
Jun 10 '13 #1
17 3976
Dormilich
8,651 Expert Mod 8TB
Can any one help?
help with what? you didnít tell what the code actually does and how it is different from what you expect.

anyways, you should take a look at the Error Console, to check whether there are any JS errors listed.
Jun 10 '13 #2
Shepard
24
@Dormilich..Thanks for the response..

It does not give any errors.. It simply displays year only...upto current year.. Month is not at all displayed..
I want it as yyyymm==201206..
Jun 10 '13 #3
Dormilich
8,651 Expert Mod 8TB
isn’t month in a separate <select>?
Jun 10 '13 #4
Shepard
24
No month should come in the same select as the year...and should come after year
Jun 10 '13 #5
Dormilich
8,651 Expert Mod 8TB
No month should come in the same select as the year
and why do you address the month and year differently, then?
Jun 10 '13 #6
Shepard
24
The problem is it does not display month.. It only displays year..
Also right now year is displayed only once...In the desired output year should be displayed as many times the month currently is...
What changes should I make in the code to get my desired output?
@Dormilich::
and why do you address the month and year differently, then?
How else should I address it?
Jun 10 '13 #7
Dormilich
8,651 Expert Mod 8TB
How else should I address it?
you said, they were the same <select>, so why should you address them differently?
Jun 10 '13 #8
Shepard
24
If I use this code
Expand|Select|Wrap|Line Numbers
  1. function populateYearSelect() 
  2.             { 
  3.                 d = new Date(); 
  4.                 curr_year = d.getFullYear(); 
  5.  
  6.                 for(i = 0; i < 2; i++) 
  7.                     document.getElementById('year').options[i] = new Option(curr_year-i,curr_year-i); 
  8.  
  9.                            } 
  10.  
It gives me years upto 2013(current year)...
Now how do I make it show month no. along with the year.. the methods I am trying is not working...
Do you know how can I get desired answer?
Jun 10 '13 #9
Dormilich
8,651 Expert Mod 8TB
hm, maybe like new Option(""+year+month, ""+year+month)?
Jun 10 '13 #10
Shepard
24
But I am not getting the month.
Jun 10 '13 #11
Dormilich
8,651 Expert Mod 8TB
how do you know?
Jun 10 '13 #12
Shepard
24
because it is not displaying it...
If you wanted this output how will you go for it?
Jun 10 '13 #13
Dormilich
8,651 Expert Mod 8TB
like in the code snippet I posted.
Jun 10 '13 #14
Shepard
24
I understand how I have to display but I dont know how to get current month in my field and decrement it.. & then when it will reach 01.. year should decrement by 1 and month should become 12..
Jun 10 '13 #15
Dormilich
8,651 Expert Mod 8TB
btw, months in Date go from 0 to 11.

and you can decrement the month by using Date.setMonth()
Jun 10 '13 #16
Rabbit
12,513 Expert Mod 8TB
You need to embed a loop in your year loop to iterate the months.
Jun 10 '13 #17
Sherin
77 64KB
I created a code which gives me yyyymm dynamically upto current year month..

Expand|Select|Wrap|Line Numbers
  1. function dropdown(monthfield, yearfield){
  2.                var monthtext=['01','02','03','04','05','06','07','08','09','10','11','12'];
  3.                var today=new Date()
  4.                var monthfield=document.getElementById(monthfield)
  5.                var yearfield=document.getElementById(yearfield)
  6.                var thisyear=today.getFullYear()
  7.                var thismonth=today.getMonth()
  8.  
  9.                var id = 0;
  10.                for (var y=0; y<=20; y++){
  11.                    for (var m=(monthtext.length-1); m>=0; m--, id++){
  12.                        if(m<=thismonth){
  13.                            yearfield.options[id]=new Option(thisyear+"-"+monthtext[m], thisyear+""+monthtext[m])
  14.                        }else{}
  15.                    }
  16.                    thisyear-=1
  17.                    thismonth = 12;
  18.                }
  19.                yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
  20.            }
  21.  
Dec 10 '20 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Ganco | last post: by
2 posts views Thread by SF | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.