Estoy usando jQuery datepicker e intenté encontrar la diferencia entre la fecha de hoy y la fecha seleccionada, pero tuve problemas… en lugar de problemas… No pude encontrarlo perfectamente…
Intenté hacer esto en ‘onSelect event of datepicker’
Pregunta: ¿Cómo verificar si la fecha seleccionada usando jQuery Datepicjer es mayor a 30 días a partir de la fecha de hoy?
Cualquier ayuda será apreciada….!! nota: no quiero usar ninguna biblioteca, necesito resolver esto usando solo jQuery.
cerbro
Obtenga la marca de tiempo de 30 días a partir de ahora:
var timestamp = new Date().getTime() + (30 * 24 * 60 * 60 * 1000)
// day hour min sec msec
Compare esa marca de tiempo con la marca de tiempo de la fecha seleccionada.
if (timestamp > selectedTimestamp) {
// The selected time is less than 30 days from now
}
else if (timestamp < selectedTimestamp) {
// The selected time is more than 30 days from now
}
else {
// -Exact- same timestamps.
}
-
Código sólido, pero OP parece estar pidiendo 30 días en el futuro, no hace 30 días. Obvio para mí, es solo un cambio de signo, pero es posible que desee aclararlo para futuros tropiezos.
– atónico
19 de junio de 2013 a las 0:22
He creado una muestra para ti.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<link rel="stylesheet" href="http://stackoverflow.com/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
Date: <input type="text" id="thedate"/>
<div id="checkDate">Check Date</div>
</body>
</html>
y js
$('#thedate').datepicker();
$('#checkDate').bind('click', function() {
var selectedDate = $('#thedate').datepicker('getDate');
var today = new Date();
var targetDate= new Date();
targetDate.setDate(today.getDate()+ 30);
targetDate.setHours(0);
targetDate.setMinutes(0);
targetDate.setSeconds(0);
if (Date.parse(targetDate ) >Date.parse(selectedDate)) {
alert('Within Date limits');
} else {
alert('not Within Date limits');
}
});
Puedes comprobar este código en línea. Aquí
Harish Ambady
Prueba esto:
$('input').datepicker({
onSelect: function()
{
var date = $(this).datepicker('getDate');
var today = new Date();
if((new Date(today.getFullYear(), today.getMonth(), today.getDate()+30))>date)
{
//Do somthing here..
}
},
});
manifestación: http://jsfiddle.net/jeY7S/