/** * [getDayPrevAfter 获得n天前/后的日期] * @param {[String]} date [日期,非必填参数,表示调用时的时间] * @param {[String]} type [前一天还是后一天,非必填参数,默认后一天] * @param {[Int]} daysNum [天数,非必填参数,默认一天] * @return {[Date]} [description] */ export function getDayPrevAfter(date, type, daysNum) { date = date ? date : new Date(); type = type ? type : 'after'; daysNum = daysNum ? daysNum : 1; if ( !(date instanceof Date) ) { if ( date.indexOf('-') != -1 ) { date.replace(/\-/g,'https://www.jb51.net/'); } date = new Date(date); } let returnDate = date; if (type === 'prev') { returnDate = new Date(date.getTime() - (daysNum * 24 * 60 * 60 * 1000)); } else if (type === 'after') { returnDate = new Date(date.getTime() + (daysNum * 24 * 60 * 60 * 1000)); } return returnDate; }
formatDateWithTimeZone
/** * [formatDateWithTimeZone 格式化日期带时区,ISO 8601] * @param {[Date]} date [日期,非必填参数,表示调用时的时间] * @return {[String]} [ISO 8601格式的日期,example: 2016-11-21T14:09:15+08:00] */ export function formatDateWithTimeZone(date) { date = date ? date : new Date(); if ( !(date instanceof Date) ) { if ( date.indexOf('-') != -1 ) { date.replace(/\-/g,'https://www.jb51.net/'); } date = new Date(date); } let tzo = -date.getTimezoneOffset(), dif = tzo >= 0 ? '+' : '-', pad = function (num) { let norm = Math.abs(Math.floor(num)); return zeroPadding(norm); }; return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}${dif}${pad(tzo / 60)}:${pad(tzo % 60)}`; }
countDownBySecond
/** * [countDownBySecond 倒计时] * @param {[Int]} restSeconds [剩余秒数,必填] * @param {[Int]} timeInterval [时间间隔,非必填,默认1000ms] * @param {[Function]} func [每倒计时一次,就需要执行一次的回调函数名,非必填] * @param {[Function]} endFun [倒计时结束需要执行的函数名,非必填] * @return {[null]} [无返回值] */ export function countDownBySecond(restSeconds, timeInterval, func, endCallback) { let timer = null; let total = restSeconds; timeInterval = timeInterval ? timeInterval : 1000; timer = setInterval(function() { --total; if (total <= 0) { clearInterval(timer); endCallback && endCallback(); } func && func(total); }, timeInterval); }
monthSize
/** * [monthSize 获得指定日期所在月的天数] * @param {[Date]} oDate [指定的日期,非必填,默认为当天] * @return {[Int]} [总天数] */ function monthSize(oDate) { oDate = oDate ? oDate : new Date(); let year = oDate.getFullYear(), month = oDate.getMonth(), _oDate = new Date(); _oDate.setFullYear(year); _oDate.setMonth(month + 1, 0); return _oDate.getDate(); }
getCalendarMonth
/** * [getCalendarMonth 获得指定日期所在月的第一周到第四/五周的数据组合,形如: * [{ "date": "2016/10/30", //日期字符串 "dateNum": 30, //日 "isCurMonth": false, //是否当前月 "weekIndex": 0 //是本月的第几周,下标从0开始 },{ "date": "2016/10/31", "dateNum": 31, "isCurMonth": false, "weekIndex": 0 },{ "date": "2016/11/1", "dateNum": 1, "day": 2, "isCurMonth": true, "isToday": false, "weekIndex": 0 }] ] * @param {[Date]} param [指定的日期,非必填,默认为当天] * @return {[Array]} [获得指定日期所在月的第一周到第四/五周的数据组合] */ export function getCalendarMonth(date) { date = date ? date : new Date(); let y = date.getFullYear(); let m = date.getMonth(); let _m; let firstDay = new Date(y, m, 1).getDay(); //当月第一天 周期 let days = monthSize(date);//当月天数 let prevMonthDays = monthSize(new Date(y, m - 1));//上月天数 let initPrevDay = prevMonthDays - firstDay; let lines = Math.ceil((firstDay + days) / 7); _m = new Array(lines * 7); let nextMonthDay = 0; for (let i = 0; i < _m.length; i++) { let weekIndex = parseInt(i / 7); if (i < firstDay) { let date = ++initPrevDay; if (m === 0 && date > 7) { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y - 1}/${12}/${date}` }; } else { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y}/${m}/${date}` }; } } else if (i >= (firstDay + days)) { let date = ++nextMonthDay; if (m === 11 && date <= 7) { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y + 1}/${1}/${date}` }; } else { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y}/${m + 2}/${date}` }; } } else { let _date = i - firstDay + 1; let today = new Date(); let today_y = today.getFullYear(); let today_m = today.getMonth(); let today_d = today.getDate(); let isToday = today_y === y && today_m === m && today_d === _date ? true : false; _m[i] = { dateNum: _date, //日期 day: i % 7, //周期 weekIndex, isCurMonth: true, isToday, date: `${y}/${m + 1}/${_date}` }; } } return _m; }
getOneDateWeekIndex