Jquery ui datepicker con EOM e mamma
Domanda
Ho bisogno di implementare un calendario JQuery UI che quando gli utenti fanno clic su qualsiasi data, seleziona automaticamente alla prossima mamma o EOM.
Ad esempio, se l'utente scatta il 9 aprile, deve andare al "15 aprile", o se clicca il 22 febbraio, deve andare a '28 febbraio' o '29 febbraio'.
Per chiarimenti: La mamma è "metà del mese, ogni volta è il 15", da 1 a 15 di qualsiasi mese. EOM è "fine del mese, è l'ultimo giorno del mese, il 31 gennaio, il 28 febbraio o il 29 marzo 2011, il 30 aprile, il 31 maggio, il 30 giugno, il 31 luglio, il 31 agosto, il 30 settembre, il 31 settembre 201131 dicembre ...
Grazie
Soluzione 3
Il codice che funziona con IE6 / IE7 / IE8 / IE9 e FF, Chrome, ecc.
<script type="text/javascript">
$(function() {
$("#payperiodcalendar input").datepicker({
dateFormat: 'dd-M-yy',
onSelect: function(dateText, inst) {
var selectedDay = inst.currentDay,
selectedMonth = inst.currentMonth,
selectedYear = inst.currentYear;
if (selectedDay <= 15) {
var now = new Date(selectedYear, selectedMonth, 15);
} else {
var now = new Date(selectedYear, selectedMonth, LastDayOfMonth(selectedYear, selectedMonth));
}
$(this).attr("value", now.format("dd-MMM-yyyy"));
}
});
function LastDayOfMonth(Year, Month) {
Month++;
return (new Date((new Date(Year, Month, 1)) - 1)).getDate();
}
});
</script>
. Altri suggerimenti
Lo farei come un gestore jQuery jquery standard per la casella di testo.Il motivo è che un utente può bypassare il selettore di data e digitare manualmente una data.Volete che la data di essere automaticamente cambiata non importa se la data è selezionata, digitata o incollata.
Utilizzare l'evento OnSelect dal Datepicker.Tieni presente che se si dispone di set di opzione Maxdate e la sua prima di EOM o MOM il valore selezionato sarà maxdate.
Aggiornamento: chiudi dopo selezionare.Usa invece onclose.
update2: jsfiddle usando l'ultima jQuery ui
onClose: function(dateText, inst) {
var day = inst.currentDay,
month = inst.currentMonth,
year = inst.currentYear;
if (day <= 15) {
day = 15;
}
else {
day = 0;
if (month>10){
month = 0;
year++;
}
else
month++;
}
var d = new Date(year, month, day);
$(this).datepicker("setDate", d);
}
.