* * 示例:$.time('yyyy-MM-dd qq HH:mm:ss.S') * :$.time('yyyyMMddHHmmssS') * y:年 M:月 d:日 q:季 H:时 m:分 s:秒 S:毫秒 * 其中y可选0-4位占位符、S可选0-1位占位符,其余可选0-2位占位符 * @param {string} fmt 格式化参数 * @param {number} 可选: 根据指定时间戳返回格式化日期 *
(fmt, ts = null)
| 269 | * |
| 270 | */ |
| 271 | time(fmt, ts = null) { |
| 272 | const date = ts ? new Date(ts) : new Date() |
| 273 | let o = { |
| 274 | 'M+': date.getMonth() + 1, |
| 275 | 'd+': date.getDate(), |
| 276 | 'H+': date.getHours(), |
| 277 | 'm+': date.getMinutes(), |
| 278 | 's+': date.getSeconds(), |
| 279 | 'q+': Math.floor((date.getMonth() + 3) / 3), |
| 280 | 'S': date.getMilliseconds() |
| 281 | } |
| 282 | if (/(y+)/.test(fmt)) |
| 283 | fmt = fmt.replace( |
| 284 | RegExp.$1, |
| 285 | (date.getFullYear() + '').slice(4 - RegExp.$1.length) |
| 286 | ) |
| 287 | for (let k in o) |
| 288 | if (new RegExp('(' + k + ')').test(fmt)) |
| 289 | fmt = fmt.replace( |
| 290 | RegExp.$1, |
| 291 | RegExp.$1.length == 1 |
| 292 | ? o[k] |
| 293 | : ('00' + o[k]).slice(('' + o[k]).length) |
| 294 | ) |
| 295 | return fmt |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * |
nothing calls this directly
no outgoing calls
no test coverage detected