Cómo habilitar el último día de cada mes en datepicker de Jquery

Si queremos habilitar sólo el último día de cada mes podemos añadir este script en el head de nuestra página web:

$(function() {
    $( "#datepicker" ).datepicker();
  });
  function LastDayOfMonth(Year, Month)
{
    return(new Date((new Date(Year, Month+1,1))-1)).getDate();
}
  $(function() {
   $('#txtDate').datepicker({
       beforeShowDay: function (date) {
        //getDate() returns the day (0-31)
        if (date.getDate() == LastDayOfMonth(date.getFullYear(),date.getMonth())) {
            return [true, ''];
        }
        return [false, ''];
    }
   });
});

Con lo que tendríamos lo siguiente para el mes de Septiembre de 2015:

Datepicker último día de cada mes

 

El código completo para que funcione en un archivo .html sería el siguiente:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  function LastDayOfMonth(Year, Month)
{
    return(new Date((new Date(Year, Month+1,1))-1)).getDate();
}
  $(function() {
   $('#txtDate').datepicker({
       beforeShowDay: function (date) {
        //getDate() returns the day (0-31)
        if (date.getDate() == LastDayOfMonth(date.getFullYear(),date.getMonth())) {
            return [true, ''];
        }
        return [false, ''];
    }
   });
});
</script>
</head>
<body>
Fecha: <input type='text' id='txtDate' />
</body>
</html>

Publicado por

victor

Hola!. Me llamo Víctor y soy Ingeniero Agrónomo de formación y programador web de profesión (desde el año 2006). Por otra parte me gusta, entre otras aficiones, el senderismo, jugar al futbol y al FIFA en la PS4. Te animo a participar en mi web www.biolucas.com

Un comentario en «Cómo habilitar el último día de cada mes en datepicker de Jquery»

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

CommentLuv badge