uni-app实现获取未来七天时间和星期几功能

例子如下:

uni-app实现获取未来七天时间和星期几功能_第1张图片

HTML:

		
			
{{item.week}}
{{item.name}}

js 生命周期执行:

created() {
			// 默认显示当天前一周的数据
			let data = []
			this.start = this.getDay(+7);
			this.end = this.getDay();
			for (let i = 6; i >= 0; i--) {
				data.push(this.getDay(+i))
			}
			var date = data.reverse()
			this.week = date;
			var date = this.week;
			var pkc = []; 
			var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
			date.forEach((item, index) => { //循坏日期
				var f = new Date(item);
				var week = f.getDay() //计算出星期几
				var str1 = item.split('/');
				var strs = str1[2];
 
				var weeks = weekday[week] 
				var time = Math.round(new Date(item) / 1000) 
				var s = {} //用于存储每个日期对象
				s.date = item;
				s.name = strs;
				s.week = weeks;
				s.times = time;
				pkc.push(s)
			})
			this.same_week = pkc;
			
			this.same_day = pkc[0].date; 
		},

methods:事件

	getDay(day) {
				var today = new Date();
				var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
				today.setTime(targetday_milliseconds); 
				var tYear = today.getFullYear();
				var tMonth = today.getMonth();
				var tDate = today.getDate();
				tMonth = this.doHandleMonth(tMonth + 1);
				tDate = this.doHandleMonth(tDate);
				return tYear + "/" + tMonth + "/" + tDate;
			},
			doHandleMonth(month) {
				var m = month;
				if (month.toString().length == 1) {
					m = month;
				}
				return m;
 
			},
			// 
 
		},

return:

                week: [],
				same_week: [], 
				same_day: '',

你可能感兴趣的:(uni-app)