vishal_g
vishal_g

Reputation: 3921

How to generate select menu ,based on some date with one year difference

I have a date in "2015-01-09" format and i want difference of 1 year that is "2014-01-09".

Now if i get the difference , want to show in select menu like this given below.

enter image description here

How can i do this using jquery or javascript.

Upvotes: 0

Views: 45

Answers (1)

sumanth10
sumanth10

Reputation: 34

Try this approach it might be use full .

 var dString = '2015, 02, 09';
 var d = new Date(2015, 12, 09);


for(i=1;i<12;i++){

  var cD = d.getDate();
  var cM = d.getMonth();
  var cY = d.getFullYear();
  if(cM==0){
  cM=12;
  }
  if(cM==12){
    cY = d.getFullYear()-1;
  }
  var Ndate = new Date(cY, cM, cD); // 2012-03-31

  Ndate.setMonth(Ndate.getMonth() - 1);



 var nM = Ndate.getMonth();
 var nY = Ndate.getFullYear();
 if(nM==0){
 nM=12;
 }
 if(nM==12){//You will get zero once the year is changed.To eliminate it
 nY=Ndate.getFullYear()-1;
 }
  console.log(cD+"-"+cM+"-"+cY+"   <-->  "+Ndate.getDate()+"-"+nM+"-"+nY);
   d = new Date(nY, nM, Ndate.getDate());//This is for repetition of same date
}

Upvotes: 1

Related Questions