uniapp怎么写获取到当前一周是周几并且是几月几日
可以使用JavaScript的Date对象来获取当前日期和时间,并通过其方法来获取当前是一周的周几和几月几日。
下面是一个获取当前一周是周几并且是几月几日的示例代码:
export default {
data() {
return {
currentWeekday: '',
currentDate: ''
};
},
mounted() {
this.getCurrentWeekday();
this.getCurrentDate();
},
methods: {
getCurrentWeekday() {
const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
const today = new Date().getDay();
this.currentWeekday = weekdays[today];
},
getCurrentDate() {
const today = new Date();
const month = today.getMonth() + 1;
const date = today.getDate();
this.currentDate = `${month}月${date}日`;
}
}
};
在上述代码中,我们使用了data选项来定义了两个变量currentWeekday和currentDate,分别用于存储当前一周的周几和几月几日。
在mounted生命周期钩子函数中,我们调用了两个方法getCurrentWeekday和getCurrentDate来获取当前的周几和日期。
getCurrentWeekday方法中,我们首先定义了一个包含所有星期的数组weekdays,然后通过new Date().getDay()来获取当前是一周的第几天,然后从weekdays数组中获取对应的星期并赋值给currentWeekday变量。
getCurrentDate方法中,我们首先创建一个新的Date对象today,然后使用其getMonth和getDate方法来获取当前的月份和日期,并将它们拼接成一个字符串赋值给currentDate变量。
最后,我们可以在页面中使用{{ currentWeekday }}和{{ currentDate }}来显示当前一周的周几和几月几日
原文地址: https://www.cveoy.top/t/topic/iUp5 著作权归作者所有。请勿转载和采集!