陪玩原始碼,與時間、日期相關的程式碼分析
獲取上/下個月日期
// 下個月 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; }
以上就是陪玩原始碼,與時間、日期相關的程式碼分析, 更多內容歡迎關注之後的文章