Vue Js Get Current Year:To get the current year in Vue.js, you can use JavaScript’s built-in Date
object. First, create a new instance of Date
, and then use the getFullYear()
method to extract the current year.
What is the method to retrieve the current year in a Vue js application?
The data property of this Vue instance is an object that contains the currentYear
property, which is set to the current year obtained using the getFullYear()
method of the Date
object.
In the template section of your Vue instance, you have a p
element that displays the current year using the mustache syntax {{ currentYear }}.
So, when the page is loaded, the Vue instance will set the currentYear
data property to the current year, and this value will be rendered in the p
element on the page.
Overall, this is a simple and effective way to display the current year using Vue.js.
Vue Js Get Current Year Example
<div id="app">
<p>The current year is {{ currentYear }}</p>
</div>
<script type="module">
const app = new Vue({
el: "#app",
data() {
return {
currentYear: new Date().getFullYear()
};
}
})
</script>