Search code examples
javascriptdatemonthcalendar

Last month's last date using Javascript


I have a textbox in which I show only month and year. So when I have June 2013, and when I look for First half, then I want the previous month's date to 14th of June. So in Javascript I was simply using

var currentmonth=doucument.getElementbyId("textbox");
var currentdate="01-"+currentmonth;// as earlier I had 1st to 15 of month.

I want to get the last months' last date that too in a format, like

"31-May-2013".

I am trying to change like:

currentdate= new Date(currentdate).get...//but not working

Solution

  • Create date object:

    var currentdate = new Date("01-"+currentmonth)
    

    Set last day of month using setFullYear, getFullYear and getMonth methods:

    currentdate.setFullYear(test.getFullYear(), test.getMonth()+1, 0)