vue获取当前时间的前七天日期
<template><div><div v-for="(item, index) in same_week" :key="index"><span>{{ item.name }}</span><span>{{ item.week }}</span></div></div></templ
·
<template>
<div>
<div v-for="(item, index) in same_week" :key="index">
<span>{{ item.name }}</span>
<span>{{ item.week }}</span>
</div>
</div>
</template>
<script>
export default {
name: '',
components: {},
props: {},
data() {
return {
same_week: [], //保存当前最新的时间
same_day: '', //当天的时间
};
},
filters: {},
computed: {},
watch: {},
created() {
// 默认显示当天前一周的数据
let data = [];
for (let i = 6; i >= 0; i--) {
// data.push(this.getDay(-i)) // 前
data.push(this.getDay(+i)) // 后
}
let date = data.reverse(); //得出一周的日期进行排序
let pkc = []; /* 用于存储展示的日期数据 */
let weekday = [
'星期日',
'星期一',
'星期二',
'星期三',
'星期四',
'星期五',
'星期六',
];
date.forEach((item, index) => {
//循坏日期
let f = new Date(item);
let week = f.getDay(); //计算出星期几
let str1 = item.split('/');
let strs = str1[0] + '/' + str1[1] + '/' + str1[2];
let weeks = weekday[week]; /* 将计算出来的时间带入数字得出中文 */
let time = Math.round(new Date(item) / 1000); //时间戳转换
let s = {}; //用于存储每个日期对象
s.date = item;
s.name = strs;
s.week = weeks;
s.times = time;
pkc.push(s);
});
this.same_week = pkc;
//pkc存储着今天的年月日星期几,时间戳等
this.same_day = pkc[0].date; //今天的数据
},
mounted() {},
beforeDestroy() {},
methods: {
getDay(day) {
let today = new Date();
let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
let tYear = today.getFullYear();
let tMonth = today.getMonth();
let tDate = today.getDate();
tMonth = this.doHandleMonth(tMonth + 1);
tDate = this.doHandleMonth(tDate);
return tYear + '/' + tMonth + '/' + tDate;
},
doHandleMonth(month) {
let m = month;
if (month.toString().length == 1) {
m = month;
}
return m;
},
},
};
</script>
<style scoped lang="less"></style>
更多推荐
已为社区贡献9条内容
所有评论(0)