Vue 获取当前日期:简单教程和示例
要得到当前的日月,可以使用 Vue 中的 Date 对象。首先,我们需要在 Vue 实例中定义一个 data 属性来存储当前日期的信息:
data() {
return {
currentDate: new Date()
}
}
然后,在模板中可以使用双花括号语法来显示当前的日月信息:
<div>{{ currentDate.getDate() }}月{{ currentDate.getMonth() + 1 }}日</div>
在上面的代码中,currentDate.getDate() 将返回当前的日期(1-31),而 currentDate.getMonth() + 1 将返回当前的月份(0-11,需要加 1 才能显示正确的月份)。
这样,当 Vue 实例初始化时,currentDate 将被设置为当前的日期和时间。在模板中,{{ currentDate.getDate() }}月{{ currentDate.getMonth() + 1 }}日 将显示当前的日月信息。
原文地址: https://www.cveoy.top/t/topic/pmOd 著作权归作者所有。请勿转载和采集!