1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| const getDate = (time) => { let date = new Date(time) let year = date.getFullYear() let month = fix0(date.getMonth() + 1) let day = fix0(date.getDate()) let h = fix0(date.getHours()) let minutes = fix0(date.getMinutes()) let seconds = fix0(date.getSeconds()) return { year, month, day, h, minutes, seconds } }
const timeformat = (time, str = 'y-m-d H:i:S') => { let obj = getDate(time * 1) if (!obj) { return null; } let result = str.replace(/([yY])/, `${obj.year}`) .replace(/([mM])/, `${obj.month}`) .replace(/([dD])/, `${obj.day}`) .replace(/([hH])/, `${obj.h}`) .replace(/([iI])/, `${obj.minutes}`) .replace(/([sS])/, `${obj.seconds}`) return result }
const fix0 = (num) => { return num < 10 ? String('0' + num) : String(num) }
|