直接上代码:
使用方法:
dateformat(\'h:m:s\') => 09:08:11
dateformat(\'y-M-d h:m:s\') => 2018-06-08 09:08:11
/** * params:{ * \'y, 年 * M, 月 * d, 日 * h, 时 * m, 分 * s\' 秒 * } * 使用方法:dateformat(\'h:m:s\') => 09:08:11 * dateformat(\'y-M-d h:m:s\') => 2018-06-08 09:08:11 */ export function dateformat(params) { var date = new Date(), year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate(), hour = date.getHours(), minute = date.getMinutes(), second = date.getSeconds() var arr = params.split(\'\') var result = \'\' for(var i = 0; i < arr.length; i += 2){ var tem = arr[i+1] === undefined ? \'\' : arr[i+1] switch(arr[i]){ case \'y\': result += addZero(year) + tem break case \'M\': result += addZero(month) + tem break case \'d\': result += addZero(day) + tem break case \'h\': result += addZero(hour) + tem break case \'m\': result += addZero(minute) + tem break case \'s\': result += addZero(second) break } } return result } // 如果时间是个位数,就补0 function addZero(obj){ return obj < 10 ? \'0\' + obj : obj }