Setting Start and End Dates in Bootstrap Vue Datepicker to Ensure End Date is After Start Date

My Vue.js form includes two BootstrapVue datepickers for holiday management. Users can define the start date and end date of their holiday, with the condition that the end date must be equal to or greater than the start date. Here is my current implementation:

<b-form-group
      id="input-group-3"
      label="Start Date:"
      label-for="input-3"
>
<b-form-datepicker
      id="input-3"
      v-model="from"
      placeholder="Select Start Date"
      required
      :min="minStart"
></b-form-datepicker>
</b-form-group>

<b-form-group id="input-group-4" label="End Date:" label-for="input-4">
<b-form-datepicker
     id="input-4"
     v-model="to"
     placeholder="Select End Date"
     :min="minEnd"
     required
></b-form-datepicker>
</b-form-group>

The data section in Vue is structured as follows:

  data() {
    const date = new Date();
    const todayDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
    
    const minFrom = new Date(todayDate);
    minFrom.setDate(minFrom.getDate()+ 1);

    const minTo = new Date(todayDate);
    minTo.setDate(minTo.getDate()+1);

    return {
        HolidayType: "",
        Reason: "",
        from: "",
        to: "",
        Status: "",
        UserID: 24,
        minStart: minFrom,
        minEnd: minTo,
      types: [
        { value: null, text: "Select Holiday Type" },
        { value: "Vacation", text: "Vacation" },
        { value: "Special Leave", text: "Special Leave" },
      ],
      reasons: [
        { value: "Moving", text: "Moving" },
        { value: "Wedding", text: "Wedding" },
        { value: "Birth", text: "Birth" },
        { value: "Other", text: "Other" },
      ],
      VacationDays: "",
    };
  }

How can I ensure that the end date is either equal to or greater than the start date?

Answer №1

Here's a codepen I made just for you: https://codepen.io/mtveerman/pen/gOwwNoL (please note that I utilized vuetify as the layout engine, but you'll grasp the concept)

What was accomplished:

  • Creation of an internalBis property
  • Establishment of a computed bis property with a get and set function:
  • Assignment of the internalBis property in the set function
  • Comparison between von and internalBis in the get function, returning von if internalBis is smaller than von

When dealing with components and form submissions, stick to using bis, as it will consistently provide the correct value. It's worth noting that if von changes to a date beyond the original bis, then bis will automatically adjust accordingly.

The most salient code section for your understanding is here:

data: () => ({
     von: new Date().toISOString().substr(0, 10),
     internalBis: new Date().toISOString().substr(0, 10)
  }),
  computed: {
    bis: {
      // getter
      get: function () {
        if (this.internalBis < this.von) {
          return this.von
        }
        return this.internalBis
      },
      // setter
      set: function (newValue) {
        this.internalBis = newValue
      }
    }
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Excessive API requests can occur when Redux dispatches an action multiple times

Utilizing the Jikan API for anime, my objective is to showcase promo thumbnails of new anime shows. This involves two separate API calls: one to retrieve the latest anime shows: export const get_new_anime = () => `${base_url}search/anime?q&order_b ...

Calculate the sum of the elements within an array that possess a distinct attribute

I need to calculate the sum of certain elements in an array. For example, let's consider this array: var sampleArray = [ {"id": 1, "value": 50, "active": true}, {"id": 2, "value": 70, "active": false}, ...

State loss occurs when moving to a different page using next/link

Currently, I am working on a library application with Next.js. Within this project, there are two main pages: BooksPage, which displays a list of all books, and BookPage, where detailed information about a specific book is shown. The components involved in ...

I am struggling to make php redirect work using onclick() function

My PHP button is not redirecting properly. Assuming the page destination is correct, is there anything else that could be causing this issue? echo "<button id=\"create\" onclick=\"location.href('/team/teams.php?op=create');&bso ...

Can anyone guide me on incorporating FontAwesome into my Vue/Express/hypernova-vue application?

I am currently developing a Rails app that utilizes hypernova-vue to integrate Vue3 micro frontend apps/components across the site. I have created a small npm library containing components that are used by the Vue component micro frontends. However, after ...

Automatically simulate the pressing of the enter key in a text field upon page load using Javascript

I am looking to simulate the pressing of the enter key in a text field when a page is loaded. Essentially, I want the text field to automatically trigger the enter key press event as if it had been pressed on the keyboard by the user. Below is an example o ...

Angular post request does not update the table

After displaying a table and a form on a page, I encountered an issue where the table does not update with new data when submitting the form. Even after refreshing the page, the new data is not reflected. As a newbie to Angular, I'm unsure of what exa ...

The deployment on Vercel is encountering an issue because it cannot find the React Icons module, even though it has been successfully installed

My attempt to deploy a project on Vercel is encountering an error during the building phase. The error message states that React icons cannot be found, even though they are installed in the package.json file and imported correctly in the component using th ...

The error message states: "TypeError: a.map is not a function"

I recently deployed my React JS app on Heroku and encountered some issues with the routing. While everything worked smoothly on my local host, I faced errors after fixing the routing problem that I couldn't resolve. Despite extensive research, I haven ...

Customize the position of the Datetimepicker

Is there a way to customize the position of the datetimepicker, ensuring that it stays within the visual boundaries without getting cut off? I need help with this. ...

Encountering a 500 server error while attempting to retrieve content from Google Images through the Web Speech API

My current project involves utilizing the Web Speech API to dynamically gather free images from Google. Here's how it works: I extract the search keyword using the Web Speech API in JavaScript. The keyword is then sent to the server (PHP) via an a ...

Encountering a strange issue when attempting to link app.js with mongodb

I recently set up MongoDB and the Compass, everything was working fine until I tried to connect MongoDB with my app.js. Now I'm getting a strange error message. Could anyone help me understand what this error means and how I can fix it? Unfortunately, ...

Discover the magic of observing prop changes in Vue Composition API / Vue 3!

Exploring the Vue Composition API RFC Reference site, it's easy to find various uses of the watch module, but there is a lack of examples on how to watch component props. This crucial information is not highlighted on the main page of Vue Composition ...

Ways to retrieve the identifier of an iframe

document.getElementById(Iframe_id).contentWindow.addEventListener("blur", blurtest, true); This line of code assigns the blur event to an iframe and it is functioning properly. However, when in function blurtest(e) { alert(e.target.id); } An alert ...

"Utilizing ng-select with ng-model: A Step-by-Step Guide

Currently, I am working on a code that involves using ng-repeat to loop through options. My goal is to utilize ng-select to choose a value based on a specific condition. However, according to the AngularJS documentation: ngSelected does not interact wit ...

Avoiding node package trigger errors in another setting

I am facing an issue with my browser extension that utilizes Webpack. The problem lies in a specific package called webextension-polyfill, which throws an error when the script is executed outside of the extension environment. Typically, this wouldn' ...

Error in typescript: The property 'exact' is not found in the type 'IntrinsicAttributes & RouteProps'

While trying to set up private routing in Typescript, I encountered the following error. Can anyone provide assistance? Type '{ exact: true; render: (routerProps: RouterProps) => Element; }' is not compatible with type 'IntrinsicAttribu ...

The Importance of Strict Contextual Escaping in ReactJS

When making an API call, we receive a URL in the response. In Angular JS, we can ensure the safety of this URL by using $sce.trustAsResourceUrl. Is there an equivalent function to trustAsResourceUrl in React JS? In Angular, //Assuming 'response&apos ...

X-editable does not verify if the checklist values are checked

Hello everyone, currently I am working on a Laravel project where I have implemented the X-editable library for inline editing functionalities. I am facing an issue with updating a many-to-many relationship table (pivot table) and I am trying to use the X- ...

Transforming jQuery Object into a String after making an AJAX request

If I were to submit a form with some text in the value of user_input, let's say "I am free," through AJAX, and it comes back to me as a string. Once it becomes an Object, how could I convert it back into a string format? Thanks, <!DOCTYPE HTML> ...