简体   繁体   中英

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

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM