我将jquery ui datepicker更改为flatpickr。但是我需要知道如何为Flatpickr做这个$('#from2').datepicker('getDate')
。我无法在网上找到如何使用Flatpickr进行操作。
谢谢你的时间。
*编辑
function getDays() {
let start = $('#from1').datepicker('getDate'),
end = $('#to1').datepicker('getDate'),
days = (end - start) / 1000 / 60 / 60 / 24;
if(days == 0 || days > 0) {
$('#howManyNights').val(days);
}
return days;
}
*不适用于flatpickr:
function getDays() {
let start = $('#from1').val();
end = $('#to1').val();
days = (end - start) / 1000 / 60 / 60 / 24;
console.log(days);
if(days == 0 || days > 0) {
$('#howManyNights').val(days);
}
return days;
}
天=南
最佳答案
通过使日期对象超出Flatpickr的值(您可以运行的工作片段),尝试这样操作
var from1 = flatpickr('#from1', {});
var to1 = flatpickr('#to1', {});
function getDays() {
var start = new Date($('#from1').val());
var end = new Date($('#to1').val());
days = (end - start) / 1000 / 60 / 60 / 24;
if(days == 0 || days > 0) {
$('#howManyNights').val(days);
}
return days;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.3/flatpickr.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.3/flatpickr.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
From:<input name="from1" id="from1" type="text">
To:<input name="to1" id="to1" type="text">
How Many:<input name="howManyNights" id="howManyNights" type="text">
<button onclick="getDays()">Get Days</button>