The data property is design for storing variables, not invoking functions or adding logic like you're trying to do.
Try computed property:
computed: {
tomorrow() {
const d = new Date()
d.setDate(d.getDate() + 1)
return d
}
}
And then in your template you can do {{ tomorrow }} or in your vue component this.tomorrow
v-model on a Date input works with a string in yyyy-MM-dd format. If you're happy to have strings in your model, not date objects, then just put your default date string in the date variable, like this.
date : new Date().toISOString().slice(0,10)
Here's a running example. A name has been changed to avoid having variable names close to reserved keywords!
vm = new Vue({
el: '#vueRoot',
data: { myDate : new Date().toISOString().slice(0,10) }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div id='vueRoot'>
<input type='date' v-model='myDate'>
<br>
{{myDate}}
</div>
Of course, you may want to have dates as date objects in your model, if you want to format or compare them. In that case, you should avoid v-model and code the two sides of your binding separately, like this.
v-model binding
<datepicker v-model="state.date" name="uniquename"></datepicker>
Emits events
<datepicker
@selected="doSomethingInParentComponentFunction"
@opened="datepickerOpenedFunction"
@closed="datepickerClosedFunction"
>
</datepicker>
You could just take the first 7 days (and make sure u start with Monday) to calculate the average temperature and use the following to formate it
const date = moment();
var FormatedDate = date.format('MMM D - ') + date.add(7, 'days').format('D YYYY');
console.log(FormatedDate);
for the days u don't need any format actually, pure HTML would be enough since the first element from the array is Monday and has the temperature for the day too. Hope i could understand your quastion
I have created two different methods, the one that already has been shown above, but outputting the full week days check below.
<div v-for="(country, index) in info" :key="index">
<template v-if="index == 0">
{{ weekDate(country.datetime) }}
</template>
<template v-else>
{{ dayFullName(country.datetime) }}
</template>
{{ Math.floor((country.min_temp + country.max_temp) / 2) }}
</div>
methods: {
weekDate (value) {
const entireWeek = moment(value).format("MMMM D -") + moment(value).add(7, 'days').format("D YYYY")
return entireWeek; // July 6 - 13 2020
},
dayFullName (date) {
const getFullName = moment(date).format('dddd');
return getFullName // Monday, Tuesday etcetera
}
}
Because the time at present isn't depend on any data variable, so we can write it in methods, and call in created
Read more about computed and methods here
You can copy and run it in CodingGround
<html>
<head>
<title>VueJs Introduction</title>
<script type = "text/javascript" src = "https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.min.js">
</script>
</head>
<body>
<div id = "intro" style = "text-align:center;">
<h1>{{ timestamp }}</h1>
</div>
<script type = "text/javascript">
var vue_det = new Vue({
el: '#intro',
data: {
timestamp: ""
},
created() {
setInterval(this.getNow, 1000);
},
methods: {
getNow: function() {
const today = new Date();
const date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
const dateTime = date +' '+ time;
this.timestamp = dateTime;
}
}
});
</script>
</body>
</html>
Assuming you have a component, like Dashboard.vue, you can use a getter:
<template>
<v-container fluid>
<h1>{{ timestamp }}</h1>
</v-container>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
@Component
export default class Dashboard extends Vue {
get timestamp() {
const today = new Date();
const date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
const timestamp = date + ' ' + time;
return timestamp;
}
}
</script>