Been looking at various solutions for this, but they all seem to be backend-related.
I am looking for a Vue/JavaScript method.
I have a string that contains ID’s. The first 6 digits are date of birth, and the final 4 are a personal number, so an ID will look something like this:
211089-0000
So the first 6 digits represent DD/MM/YYYY.
I have two fields: one where the ID is entered and one that contains date of birth. I would like for the date of birth field to be updated when an ID has been entered, using the first 6 digits and converting them into a date format.
This is how far I have gotten:
I have a computed method that looks like this:
updateDateOfBirth(){
if(!this.dateOfBirth && this.userId > 6){
return this.dateOfBirth = this.userId.substring(0,6)
}
}
And I added it as a change event in the userId input form
<template>
<div>
<b-form-input v-model="userId" @change="updateDateOfBirth" placeholder="Enter your ID"></b-form-input>
<div class="mt-2">Value: {{ userId }}</div>
</div>
<div>
<b-form-datepicker id="dateOfBirth" v-model="dateOfBirth" class="mb-2"></b-form-datepicker>
<p>Value: '{{ dateOfBirth }}'</p>
</div>
</template>
But I need to convert the 5th and 6th digits into a year format, so 211089 becomes 21/10/1989.
Is it something that can be done by moment.js maybe? I have not been able to find any good examples.
2
Answers
You can use moment js
Vue SFC Playground
I did it in Composition API, just port to Options API, and change ot your date picker format, I used a standard
input[type=date]
.watch
instead of@change
since you already have a model for the ID inputDate