0

I want to get the last day of the month using daterangepicker.

Here is my script:

$('input[name="enddate"]').daterangepicker({
    singleDatePicker: true,
    showDropdowns: true,
    isInvalidDate: function( date ) {
        if( date.date() === getLastDayOfYearAndMonth(date.getFullYear(), date.getMonth()) ) {
            return false; 
        } else {
            return true;
        }
    },
    locale: {
        format: 'DD-MMM-YY'
    }
});

function getLastDayOfYearAndMonth(year, month) {
    return(new Date((new Date(year, month + 1, 1)) - 1)).getDate();
}
Ignacio Ara
  • 2,476
  • 2
  • 26
  • 37

2 Answers2

0

try this to get last date using month and year:

console.log(getLastDayOfYearAndMonth(2018, 2));
function getLastDayOfYearAndMonth(year, month)
{
var d = new Date(year, month, 0);
var lastdate =d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear();
return lastdate;
}

Edited answer : 27-7-2018 :

month + 1 mean if february is selected then it will take february + 1 = march so it will take less than first day of the march ,so Setting day parameter to 0 means one day less than first day of the month which is last day of the previous month

like explain here

jQuery(function($) {
  $('input[name="daterange"]').daterangepicker({
    singleDatePicker: true,
    showDropdowns: true,
    isInvalidDate: function( date ) {
   
var mydate = new Date(date);
var lastday = getLastDayOfYearAndMonth(mydate.getFullYear(), mydate.getMonth());
      if( date.date() === lastday) {
        return false; 
      } else {
        return true;
      }
    },
    locale: {
      format: 'DD-MMM-YY'
    }
  });
 
  
  //alert(getLastDayOfYearAndMonth('2018', '07'));
  function getLastDayOfYearAndMonth(year, month)
  {
    var d = new Date(year, month+1, 0);
    var lastdate =d.getDate();
    return lastdate;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link href="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" rel="stylesheet"/>
<input type="text" name="daterange" />
Kamalesh M. Talaviya
  • 1,422
  • 1
  • 12
  • 26
0
console.log(getLastDay(2018,7));

    function getLastDay(year,month) {        
    // GET THE MONTH AND YEAR OF THE SELECTED DATE.        
    var LastDay = new Date(year, month + 1, 0);
    // FINALLY, GET THE DAY.
    var weekday = new Array();
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    return weekday[LastDay.getDay()] +' (' + 
    LastDay.toDateString('dd/mon/yyyy') + ')';
}
codeLover
  • 2,571
  • 1
  • 11
  • 27
swathi
  • 97
  • 1
  • 2
  • Hi i put a sample code. the alert message shows last day of july. the result of the message will be put on the if conditions located on lastday: code here: https://jsfiddle.net/rLnycn80/1290/ – Rrj Villareal Jul 27 '18 at 06:59