Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Monday 3 December 2012

Get First And Last Date Of Month Using Javascript

A couple of days ago I need to get the first and last dates of the month by using Javascript so while searching I come up with a Javascript function which was pretty helpful to me so I decided to share it with you guys. Here we go:

JavaScript Function To Get First and Last Date :


function getFirstLastDates(){
            var i=0;
            // get the current date
            var j=new Date();
            var first;
            var last;
            // loop will not break untill i will be equal to 12
           while(i!=12)
           {
              // get the first date of the month.
              first = new Date(j.getFullYear(), i, 1);
              // get the last date of the month.
              last= new Date(j.getFullYear(), i+1, 0);
              alert(first);
              alert(last);
              i++;
           }
     }

In the above function what we did is that just get the current date and loop 12 times because the total months are 12. In the loop we use first= new Date (j.getFullYear (), i, 1);   or first= new Date (year, month, day) to get the first and last date.


No comments:

Post a Comment

Please do not enter spam links.