12345678910111213141516171819202122232425 |
- /*
- * @Description: 时间数据格式
- * @Autor: kindring
- * @Date: 2021-10-14 18:53:00
- * @LastEditors: kindring
- * @LastEditTime: 2021-10-14 19:08:37
- * @LastDescript:
- */
- function dateFormat(date, format = 'YYYY-MM-DD HH:mm:ss') {
- const config = {
- YYYY: date.getFullYear(),
- MM: ('0' + (date.getMonth() + 1)).substr(-2, 2), //getMonth() 方法根据本地时间返回指定日期的月份(从 0 到 11)
- DD: ('0' + date.getDate()).substr(-2, 2),
- HH: ('0' + date.getHours()).substr(-2, 2),
- mm: ('0' + date.getMinutes()).substr(-2, 2),
- ss: ('0' + date.getSeconds()).substr(-2, 2),
- }
- console.log(config);
- for (const key in config) {
- format = format.replace(key, config[key])
- }
- return format
- }
- console.log(dateFormat(new Date()));
|