1 /** 2 * 获取年月,如:2018-08 3 */ 4 export function getMonth () { 5 return formatDate(new Date(), 'yyyy-MM') 6 } 7 8 /** 9 * 10 * @param {*} date 11 * @param {*} fmt : yyyy-MM、yyyy-MM-dd、yyyy-MM-dd hh:mm、yyyy-MM-dd hh:mm:ss 12 */ 13 export function formatDate (date, fmt) { 14 // 获取年份,替换fmt中的yyyy部分 15 if (/(y+)/.test(fmt)) { 16 fmt = fmt.replace(RegExp.$1, date.getFullYear().toString().substr(4 - RegExp.$1.length)) 17 } 18 let f = { 19 'M+': date.getMonth() + 1, 20 'd+': date.getDate(), 21 'h+': date.getHours(), 22 'm+': date.getMinutes(), 23 's+': date.getSeconds() 24 } 25 for (let key in f) { 26 if (new RegExp(`(${key})`).test(fmt)) { 27 let str = f[key].toString() 28 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : str.padLeft(2, '0')) 29 } 30 } 31 return fmt 32 }