陪玩源码,与时间、日期相关的代码分析
获取上/下个月日期
// 下个月 let date = new Date(this.startDate); let nextMonthDate = date.setMonth(date.getMonth() + 1); this.endDate = dateFormat("YYYY-MM-DD", new Date(nextMonthDate)); // 上个月 let lastMonthDate = date.setMonth(date.getMonth() - 1);
获取前/后7天日期
// 前7天 let date = new Date(this.startDate); let afterDate = date.setDate(date.getDate() + 6); this.endDate = dateFormat("YYYY-MM-DD", new Date(afterDate)); // 后7天 let afterDate = date.setDate(date.getDate() - 6);
一周日期
let weeks=["周一","周二","周三","周四","周五","周六","周日"]; for (let i = 0; i<7; i++){ let date = new Date(); let index = date.getDay() ? date.getDay() - 1 : 6; let nowDate = date.setDate(date.getDate() - index + i); let formatDate = dateFormat("MM-DD", new Date(nowDate)); let week = weeks[i]; let weekDate = `${formatDate}(${week})`; this.weekDateColumns.push(weekDate); }
当月第一天和最后一天
// 第一天 let date = new Date(); date.setDate(1); console.log(dateFormat("YYYY-MM-DD", date)); // 最后一天 let date = new Date(); let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0); console.log(dateFormat("YYYY-MM-DD", lastDay));
计算传入时间与当前时间相差的秒数
function convertStrToTime (time) { let nowTime = new Date().getTime(); let date = new Date(); let arr = time.split(":"); date.setHours(parseInt(arr[0])); date.setMinutes(parseInt(arr[1])); let endTime = new Date(date).getTime(); let ret = Math.ceil((endTime - nowTime) / 1000); return ret; }
以上就是陪玩源码,与时间、日期相关的代码分析, 更多内容欢迎关注之后的文章
标签:代码,Date,日期,源码,let,date,new From: https://www.cnblogs.com/yunbaomengnan/p/18312716