如何从 flatpickr 获取日期输入?
How to get the date input from flatpickr?
我正在将 jquery ui 日期选择器更改为 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>
我正在将 jquery ui 日期选择器更改为 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>