ฉันใช้ bootstrap datepicker บนแบบฟอร์มด้วย ajax เพื่ออัปเดตขีดจำกัดวันที่ใน datepicker
ฉันกด ajax เพื่อรับจำนวนวันทั้งหมดและ setEndDate ของ datepicker เพื่อจำกัดวันที่บน (วันที่ภายหลังทั้งหมดจะถูกปิดใช้งาน)
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
success: function (data) {
if('total' in data && data['total'] > 1) {
// datepicker successfully set the limit to next 'total' days
$datepicker.datepicker('setEndDate', '+'+data['total']+'d');
// how to set the field value with that end date?
$datepicker.val($datepicker.datepicker('getEndDate'));
}
},
error: function (e) {
console.log(e);
}
});
คำถามของฉันคือจะจัดรูปแบบ $datepicker.datepicker('getEndDate')
เป็น yyyy-mm-dd
ได้อย่างไร ขณะนี้มันส่งคืน Fri Jul 27 2018 08:00:00 GMT+0800 (Singapore Standard Time)
ฉันได้ตรวจสอบเอกสารประกอบ [1] แล้ว แต่ไม่มีพารามิเตอร์ที่จะกำหนดในการจัดรูปแบบ getEndDate
ชื่นชมความช่วยเหลือใด ๆ ขอบคุณ.
แก้ไข:
สำหรับใครก็ตามที่มีปัญหากับฉัน ขณะนี้วิธีแก้ปัญหาของฉันกำลังแยกวิเคราะห์การส่งคืนของ $datepicker.datepicker('getEndDate')
ด้วยตนเอง
ฉันเห็นเอาต์พุตเท่ากับ Date .toString()
ดังนั้นฉันจึงส่งเอาต์พุตเป็นตัวสร้างวันที่ของ JavaScript จากนั้นจึงจัดรูปแบบด้วยตนเอง
var d = new Date($datepicker.datepicker('getEndDate'));
if(d instanceof Date && !isNaN(d)) {
var day = '' + d.getDate(),
month = '' + (d.getMonth() + 1),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
// console.log([year, month, day].join('-'));
$datepicker.datepicker('update', [year, month, day].join('-'));
}
[1]http://bootstrap-datepicker.readthedocs.io/en/latest/methods.html