You can get the current date and time in Vue.js by creating a new Date
object and formatting it using the toLocaleString()
method. Here’s an example:
<template>
<div>
<p>The current date and time is {{ currentDateTime }}.</p>
</div>
</template>
<script>
export default {
data() {
return {
currentDateTime: ''
}
},
created() {
this.currentDateTime = new Date().toLocaleString();
}
}
</script>
In this example, we have a component with a currentDateTime
data property. When the component is created, we set the value of currentDateTime
to the current date and time by creating a new Date
object and calling the toLocaleString()
method on it. The resulting string is then displayed in the component’s template using the mustache syntax {{ currentDateTime }}
.
Note that the toLocaleString()
method formats the date and time string based on the user’s locale. If you want to use a specific date and time format, you can use a library like Moment.js or date-fns to format the date and time string.