Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  175] [ 2]  / answers: 1 / hits: 15882  / 5 Years ago, fri, april 19, 2019, 12:00:00

I am attempting to set a range of dates to allow users to pick the date using vue-datepicker, since desktop safari users have problems using input type date. Going thru the documentation of vue-datepicker and looking at the demo it has, I got really confused. Where do I apply the disable data to limit the range of dates on the datepicker dropdown?



I have tried to put the :disabled=disabled in datepicker, but documentation says it expects a boolean response. Any help is greatly appreciated!



<template>
<div>
<datepicker v-model=booking_date></datepicker>
</div>
</template>
<script>
import Datepicker from 'vuejs-datepicker';
import moment from 'moment';
export default {
data() {
return {
moment: moment,
booking_date: null,
disabled: {},
}
},
mounted() {
this.defaultDateRange();
},
methods: {
defaultDateRange() {
let tzoffset = new Date().getTimezoneOffset() * 60000;
let today = (new Date(Date.now() - tzoffset)).toISOString().substr(0, 10);
let max = new Date();
max.setDate(max.getDate() + 30);
let max_date = max.toISOString().substr(0, 10);
this.disabled = {
to: Date.parse(today),
from: Date.parse(max_date)
};
this.booking_date = Date.parse(today);
}
}
}
</script>


new attempt:



<datepicker v-model=booking_form.booking_date :disabled-dates=disabled></datepicker>

More From » vue.js

 Answers
24

After trying a lot the following code worked for me(In which datepicker disabled all the dates which come after 1 month from present as well as all previous dates). I have used ranges as mentioned in datepicker documentation datepicker npm



template:::



<datepicker :disabledDates=disabledDates ></datepicker>


script::::



import Datepicker from 'vuejs-datepicker';
export default {
data(){
return {
disabledDates:{
ranges:[]
}
}
},
mounted() {
this.defaultDateRange();
},
components:{
Datepicker
},
methods:{
let tzoffset = new Date().getTimezoneOffset() * 60000;
let today = (new Date(Date.now() - tzoffset));

let oldToday = new Date(today.getTime()); // AS DATES ARE REFRENCE COOPIED I HAD TO COPY THE VALUE OF TODAY
oldToday.setDate(oldToday.getDate()-1);

today.setMonth(today.getMonth()+1); // GETTING NEXT MONTHS DATE

let max = new Date(); // YOU CAN REMOVE THIS MAX VARIABLE I JUST PUT IT FOR YOUR REFRENCE
let obj = {};
max.setDate(max.getDate() + 30);
let max_date = max;

obj[from] = new Date(0,0,0); // FOR DISABLING ALL PREVIOUS DATES I PUT THIS IN RANGES ARRAY INSIDE DISABLEDDATES OBJECT
obj[to] = oldToday;

this.disabledDates[ranges].push(obj);
this.disabledDates[from] = today;
console.log(disableDates is );
console.log(this.disabledDates);
this.booking_date = Date.parse(today);
}
}

[#52222] Friday, April 12, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aliah

Total Points: 118
Total Questions: 132
Total Answers: 94

Location: Tajikistan
Member since Fri, Sep 11, 2020
4 Years ago
;