I have an object in vue js retrieved from form with attribute date and a value 22-02-2022. How can I change this value to be 2022-02-22?
Asked
Active
Viewed 430 times
-1
-
[Old question on SO with solutions](https://stackoverflow.com/questions/2388115/get-locale-short-date-format-using-javascript) , [JS Intl object formatting methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) – Art Feb 22 '22 at 07:08
-
What kind of form, under which regional settings and what kind of conversion, i.e. where (client/backend)? Your question lacks context. – Gert Arnold Feb 22 '22 at 21:08
3 Answers
0
I use moment.js. Here's an example. I usually put it on computed (U can put it as method and use some parameters to make it more flexible).
dateFormat() {
return moment(this.yourObject.date).format("YYYY-MM-DD")
},

Ananda Vj
- 172
- 10
0
I tried with an input with attribute date and the value om getting is already in yyyy-mm-dd format
<input type="date" class="form-control" v-model="birthdate" />
But you can perform the conversion yourself to fit your requirement with below example
watch:{
birthdate: function(){
console.log("Value from input "+this.birthdate)
let tempArr = this.birthdate.split("-")
let formattedDate = tempArr[2]+"-"+tempArr[1]+"-"+tempArr[0]
console.log("Formatted: "+formattedDate)
}}
Output

Siti Aisah
- 303
- 1
- 7
0
You can turn value into array and reverse them
let str = '22-02-2022'.split("-").reverse().join("-");
console.log(str);