Wednesday 31 March 2021

Vue.js Change the Value of a Data item from a Method.

This is pretty simple but I found it difficult to search for on Google. So just noting this down here in case I forget it. So I have a value in my ‘data’ that’s being output in the template. But I need to manipulate it and run a function on it. For this, we can put the functions in ‘methods'.

export default
props: {
survey: {
type: Object,
required: true
}
},
data(){
return{
survey_date: new Date(this.convert_date(                 this.survey.survey_date)).toISOString().substr(0, 10),
}
},
methods: {
convert_date(value){
console.log(this.formatDate(value))
return this.formatDate(value)
},
formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;

return [year, month, day].join('-');
}

 

No comments: