//获取系统的时间 var days = [] const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } //当前时间-数组模式 年月日 时分秒 const nowDateTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() const nowTime = [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')//整合时间 const time = { 'year': year, 'month': month, 'day': day, 'hour': hour, 'minute': minute, 'second': second, 'nowTime': nowTime } return time } //当前时间-数组模式 年月日 const nowDate = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return [year, month, day].map(formatNumber).join('-') } // 从 todate 开始计算 days 天的日期数组 function dateArry(todate,days) { var dateArry = []; for (var i = 0; i < days; i++) { var dateObj = dateLater(todate, i); dateArry.push(dateObj) } return dateArry; } function dateLater(dates, later) { let dateObj = {}; let day_week = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六'); let day_index = new Array(7, 1, 2, 3, 4, 5, 6); let date = new Date(dates); date.setDate(date.getDate() + later); let day = date.getDay(); let yearDate = date.getFullYear(); let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1); let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate()); days = days.concat(dayFormate) dateObj.date = yearDate + '-' + month + '-' + dayFormate; dateObj.week = day_week[day]; dateObj.num = day_index[day]; dateObj.day = days[day]; return dateObj; } //往后/前推几天的日期 function delayDate(today, addDayCount) { var dd; if (today) { dd = new Date(today); } else { dd = new Date(); } dd.setDate(dd.getDate() + addDayCount);//获取addDayCount天后的日期 var y = dd.getFullYear(); var m = dd.getMonth() + 1;//获取当前月份的日期 var d = dd.getDate(); if (m < 10) { m = '0' + m; }; if (d < 10) { d = '0' + d; }; var newDay = y + '-' + m + '-' + d return newDay; } //当前周的数组 function nowWeek(day) { var nowWeekArry = {} , nowDate = day //当前日期 , num = dateArry(nowDate,1)[0].num //当前周几 , monday = delayDate(nowDate, -(num - 1)) //本周一的 日期 , sunday = delayDate(nowDate, 7 - num) //本周日的 日期 , nowWeek = dateArry(monday,7) //本周列表 console.log(num) nowWeekArry = { 'nowDate': nowDate //当前日期 , 'num': num //当前周几 , 'nowWeek': nowWeek //本周列表 } return nowWeekArry } //一周的几号重组 function weekData(weekData) { var weekData = weekData for (let i = 0; i < weekData.length; i++) { var day = weekData[i].date weekData[i].day = day.substr(8, 2) } return weekData; } //日期转整型 function date(date) { var a = parseInt(date.substr(0, 4) + date.substr(5, 2) + date.substr(8, 2)) return a; } function toFix(value) { return value.toFixed(2) // 此处2为保留两位小数,保留几位小数,这里写几 } module.exports = { nowDateTime: nowDateTime //年月日 时分秒 , nowDate: nowDate //年月日 , dateArry: dateArry // 从 todate 开始计算 days 天的日期数组 , delayDate: delayDate //往后/前推几天的日期 , date: date //日期转整型 , weekData: weekData //一周的几号重组 , nowWeek: nowWeek , toFix: toFix //,box: box }