Proper way to change Bootstrap-Vue text field into mm/dd/yyyy format

7.2k Views Asked by At

I'm using Bootstrap-Vue <b-form-datepicker> component and looking for a way to customise the input date field to mm/dd/yyyy format. Any proper ways ?

<b-input-group class="mb-3">
        <b-form-input
          id="example-input"
          v-model="dateOfBirth"
          type="text"
          placeholder="MM-DD-YYYY"
          locale="en-US"
          autocomplete="off"
        ></b-form-input>
        <b-input-group-append>
          <b-form-datepicker
            v-model="dateOfBirth"
            button-only
            right
            locale="en-US"
            :date-format-options="{ year: 'numeric', month: 'short', day: '2-digit', weekday: 'short' }"
            aria-controls="example-input"
          ></b-form-datepicker>
        </b-input-group-append>
      </b-input-group>

Documentation https://bootstrap-vue.org/docs/components/form-datepicker

2

There are 2 best solutions below

1
Noah Stahl On

Does setting each component as numeric not achieve the right format?

<b-form-datepicker
  :date-format-options="{ year: 'numeric', month: 'numeric', day: 'numeric' }"
  locale="en"
></b-form-datepicker>

Gives: 9/16/2020

1
Rohìt Jíndal On

You can achieve it by simply assign the selectedFormatted value from b-form-datepicker into the v-model value of b-form-input.

Note : Use different v-model value for both b-input-group & b-form-datepicker

Demo :

new Vue({
  el: '#app',
  data() {
    return {
      value: '',
      inputValue: ''
    }
  },
  methods: {
    onContext(ctx) {
      this.inputValue = ctx.selectedFormatted;
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/>
<link rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<div id="app">
  <label for="example-input">Choose a date</label>
  <b-input-group class="mb-3">
    <b-form-input
      id="example-input"
      v-model="inputValue"
      type="text"
      placeholder="MM/DD/YYYY"
      autocomplete="off"
    ></b-form-input>
    <b-input-group-append>
      <b-form-datepicker
        v-model="value"
        button-only
        right
        locale="en-US"
        aria-controls="example-input"
        :date-format-options="{ year: 'numeric', month: 'numeric', day: 'numeric' }"
        @context="onContext"
      ></b-form-datepicker>
    </b-input-group-append>
  </b-input-group>
</div>