UNPKG

simple-frame-unit

Version:

基于vue2 研发的与simple-data后端配合使用的组件

316 lines (293 loc) 8.21 kB
// yyyy-MM-dd hh:mm:ss.SSS 所有支持的类型 function pad(str, length = 2) { str += '' while (str.length < length) { str = '0' + str } return str.slice(-length) } const parser = { yyyy: (dateObj) => { return pad(dateObj.year, 4) }, yy: (dateObj) => { return pad(dateObj.year) }, MM: (dateObj) => { return pad(dateObj.month) }, M: (dateObj) => { return dateObj.month }, dd: (dateObj) => { return pad(dateObj.day) }, d: (dateObj) => { return dateObj.day }, hh: (dateObj) => { return pad(dateObj.hour) }, h: (dateObj) => { return dateObj.hour }, mm: (dateObj) => { return pad(dateObj.minute) }, m: (dateObj) => { return dateObj.minute }, ss: (dateObj) => { return pad(dateObj.second) }, s: (dateObj) => { return dateObj.second }, SSS: (dateObj) => { return pad(dateObj.millisecond, 3) }, S: (dateObj) => { return dateObj.millisecond }, } // 这都n年了iOS依然不认识2020-12-12,需要转换为2020/12/12 export function getDate(time) { if (time instanceof Date) { return time } switch (typeof time) { case 'string': { // 2020-12-12T12:12:12.000Z、2020-12-12T12:12:12.000 if (time.indexOf('T') > -1) { return new Date(time) } // ios时间格式转换 if (time.includes('-')) { return new Date(time.replace(/-/g, '/')) } // yyyyMMdd if (isNumeric(time) && isDate(time)) { return parse(time) } // 时间戳 if (isNumeric(time) && /^\d{10}$|^\d{13}$/.test(time)) { let t = time.length === 10 ? time * 1000 : Number(time) return new Date(t) } } default: return new Date(time) } } export function formatDate(date, format = 'yyyy/MM/dd hh:mm:ss') { if (!date && date !== 0) { return '' } date = getDate(date) const dateObj = { year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate(), hour: date.getHours(), minute: date.getMinutes(), second: date.getSeconds(), millisecond: date.getMilliseconds() } const tokenRegExp = /yyyy|yy|MM|M|dd|d|hh|h|mm|m|ss|s|SSS|SS|S/ let flag = true let result = format while (flag) { flag = false result = result.replace(tokenRegExp, function (matched) { flag = true return parser[matched](dateObj) }) } return result } export function friendlyDate(time, {locale = 'zh', threshold = [60000, 3600000], format = 'yyyy/MM/dd hh:mm:ss'}) { if (!time && time !== 0) { return '' } if (time === '-') { time = new Date().getTime() } const localeText = { zh: { year: '年', month: '月', day: '天', hour: '小时', minute: '分钟', second: '秒', ago: '前', later: '后', justNow: '刚刚', soon: '马上', template: '{num}{unit}{suffix}' }, en: { year: 'year', month: 'month', day: 'day', hour: 'hour', minute: 'minute', second: 'second', ago: 'ago', later: 'later', justNow: 'just now', soon: 'soon', template: '{num} {unit} {suffix}' } } const text = localeText[locale] || localeText.zh let date = getDate(time) let ms = date.getTime() - Date.now() let absMs = Math.abs(ms) if (absMs < threshold[0]) { return ms < 0 ? text.justNow : text.soon } if (absMs >= threshold[1]) { return formatDate(date, format) } let num let unit let suffix = text.later if (ms < 0) { suffix = text.ago ms = -ms } const seconds = Math.floor((ms) / 1000) const minutes = Math.floor(seconds / 60) const hours = Math.floor(minutes / 60) const days = Math.floor(hours / 24) const months = Math.floor(days / 30) const years = Math.floor(months / 12) switch (true) { case years > 0: num = years unit = text.year break case months > 0: num = months unit = text.month break case days > 0: num = days unit = text.day break case hours > 0: num = hours unit = text.hour break case minutes > 0: num = minutes unit = text.minute break default: num = seconds unit = text.second break } if (locale === 'en') { if (num === 1) { num = 'a' } else { unit += 's' } } return text.template.replace(/{\s*num\s*}/g, num + '').replace(/{\s*unit\s*}/g, unit).replace(/{\s*suffix\s*}/g, suffix) } export function getNowDate() { let date = new Date() let y = date.getFullYear() let m = date.getMonth() + 1 let d = date.getDate() m = m < 10 ? "0" + m : m d = d < 10 ? "0" + d : d return y + "" + m + "" + d } export function getFullYear(time) { return getDate(time).getFullYear() } export function getMonth(time) { return getDate(time).getMonth() + 1 } export function getDay(time) { return getDate(time).getDate() } // 获取yyymmdd或者yyymm时间 export function parse(str) { var y = str.substr(0, 4), m = str.substr(4, 2) - 1, d = str.substr(6, 2) ? str.substr(6, 2) : 1; var D = new Date(y, m, d); return (D.getFullYear() == y && D.getMonth() == m && D.getDate() == d) ? D : 'invalid date'; } // 判断字符串是否为6or8 export function isDate(sDate) { if (!/^\d{6}$|^\d{8}$/.test(sDate)) { return false; } var year, month, day; year = sDate.substring(0, 4); month = sDate.substring(4, 6); day = sDate.substring(6, 8); var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if (year < 1700 || year > 2500) return false if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29; if (month < 1 || month > 12) return false if (/^\d{8}$/.test(sDate) && (day < 1 || day > iaMonthDays[month - 1])) return false return true } // 判断字符串是否为数字 function isNumeric(str) { return /^\d+$/.test(str); } /** * * @param {Date or String} date1 日期1 * @param {Date or String} date2 日期2 * @returns number */ export function monthNumber(date1, date2) { const dateOne = getDate(date1); const dateTwo = getDate(date2); // 第一个日期的年和月 const yearOne = dateOne.getFullYear(); const monthOne = dateOne.getMonth() + 1; // 第二个日期的年和月 const yearTwo = dateTwo.getFullYear(); const monthTwo = dateTwo.getMonth() + 1; // 两个日期的月份数 const oneMonthNum = yearOne * 12 + monthOne; const twoMonthNum = yearTwo * 12 + monthTwo; return (yearTwo - yearOne) * 12 + (monthTwo - monthOne) + (yearTwo - yearOne + 1) } /** * 获取下一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ export function getNextMonth(date) { const year = getDate(date).getFullYear(); //获取当前日期的年份 const month = getDate(date).getMonth() + 1; //获取当前日期的月份 const day = getDate(date).getDate(); //获取当前日期的日 let year2 = year; let month2 = month + 1; if (month2 === 13) { year2 = year2 + 1; month2 = 1; } let day2 = day; let days2 = new Date(year2, month2, 0); days2 = days2.getDate(); if (day2 > days2) { day2 = days2; } if (month2 < 10) { month2 = '0' + month2; } return getDate(year2 + '-' + month2 + '-' + day2); }