// 获取当前年月日的示例代码
// 使用 JavaScript 的 Date 对象来获取当前日期
new Vue({
el: '#app',
data() {
return {
currentDate: ''
};
},
created() {
// 在组件创建时获取当前日期
this.getCurrentDate();
},
methods: {
getCurrentDate() {
const now = new Date(); // 创建一个表示当前日期和时间的 Date 对象
const year = now.getFullYear(); // 获取当前年份
const month = String(now.getMonth() + 1).padStart(2, '0'); // 获取当前月份,注意月份是从0开始的,所以要加1,并且用 padStart 确保是两位数
const day = String(now.getDate()).padStart(2, '0'); // 获取当前日期,并确保是两位数
// 将年月日组合成字符串
this.currentDate = `${year}-${month}-${day}`;
}
}
});
Date
对象来获取当前的日期和时间。created
生命周期钩子中调用 getCurrentDate
方法,以确保在组件初始化时获取当前日期并更新 currentDate
数据属性。上一篇:vue 字符串拼接
下一篇:vue tree view
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站