js时间格式化
2021-04-07 本文已影响0人
糖小羊儿
照抄element-ui的时间格式化,将当前时间或者自定义时间格式化为自己需要的格式
dateFormat.js 代码
function pad(val, len) {
val = String(val);
len = len || 2;
while (val.length < len) {
val = '0' + val;
}
return val;
}
var formatFlags = {
D: function (dateObj) {
return dateObj.getDay();
},
DD: function (dateObj) {
return pad(dateObj.getDay());
},
d: function (dateObj) {
return dateObj.getDate();
},
dd: function (dateObj) {
return pad(dateObj.getDate());
},
M: function (dateObj) {
return dateObj.getMonth() + 1;
},
MM: function (dateObj) {
return pad(dateObj.getMonth() + 1);
},
yy: function (dateObj) {
return pad(String(dateObj.getFullYear()), 4).substr(2);
},
yyyy: function (dateObj) {
return pad(dateObj.getFullYear(), 4);
},
h: function (dateObj) {
return dateObj.getHours() % 12 || 12;
},
hh: function (dateObj) {
return pad(dateObj.getHours() % 12 || 12);
},
H: function (dateObj) {
return dateObj.getHours();
},
HH: function (dateObj) {
return pad(dateObj.getHours());
},
m: function (dateObj) {
return dateObj.getMinutes();
},
mm: function (dateObj) {
return pad(dateObj.getMinutes());
},
s: function (dateObj) {
return dateObj.getSeconds();
},
ss: function (dateObj) {
return pad(dateObj.getSeconds());
},
S: function (dateObj) {
return Math.round(dateObj.getMilliseconds() / 100);
},
SS: function (dateObj) {
return pad(Math.round(dateObj.getMilliseconds() / 10), 2);
},
};
var token = /d{1,4}|M{1,4}|yy(?:yy)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
var literal = /\[([^]*?)\]/gm;
function format(dateObj, mask = 'yyyy-MM-dd') {
if (!dateObj) {
dateObj = new Date();
}
if (Object.prototype.toString.call(dateObj) !== '[object Date]' || isNaN(dateObj.getTime())) {
dateObj = new Date();
}
var literals = [];
mask = mask.replace(literal, function ($0, $1) {
literals.push($1);
return '@@@';
});
mask = mask.replace(token, function ($0) {
return $0 in formatFlags ? formatFlags[$0](dateObj) : $0.slice(1, $0.length - 1);
});
return mask.replace(/@@@/g, function () {
return literals.shift();
});
}
//给当前时间格式化
function currentDateFormat(mask) {
return format(new Date(), mask);
}
//给自定义时间格式化
function customDateFormat(datestr, mask) {
let dateObj = null;
if (!datestr) {
dateObj = new Date();
} else {
dateObj = new Date(datestr);
}
return format(dateObj, mask);
}
export {
currentDateFormat,
customDateFormat,
};
方法
import { currentDateFormat, customDateFormat } from "@/util/dateFormat.js";
- currentDateFormat 对当前时间进行格式化
- 格式化格式,默认'yyyy-MM-dd',最全格式为 'yyyy-MM-dd HH:mm:ss'
- customDateFormat 对自自定义时间进行格式化
- 想要转换的日期字符串,例如'2020-10-01 08:30:00'
- 格式化格式,默认'yyyy-MM-dd',最全格式为 'yyyy-MM-dd HH:mm:ss'
例子
//currentDateFormat
currentDateFormat(); //2021-04-07
currentDateFormat("yyyy-MM-dd HH:mm:ss"); //2021-04-07 10:03:23
currentDateFormat("yyyy-MM-dd HH:mm"); //2021-04-07 10:03
currentDateFormat("yyyy-MM-dd HH"); //2021-04-07 10
currentDateFormat("yyyy-MM-dd"); //2021-04-07
currentDateFormat("yyyy-MM"); //2021-04
currentDateFormat("yyyy"); //2021
currentDateFormat("MM-dd"); //04-07
currentDateFormat("MM"); //04
currentDateFormat("dd"); //07
currentDateFormat("HH:mm:ss"); //10:03:23
currentDateFormat("HH:mm"); //10:03
currentDateFormat("HH"); //10
currentDateFormat("mm:ss"); //03:23
currentDateFormat("mm:ss"); //03:23
//customDateFormat
customDateFormat(); // 2021-04-07
customDateFormat("2010-10-18 19:17:54"); //2010-10-18
customDateFormat("2010-10-18 19:17:54", "yyyy-MM"); //2010-10
customDateFormat("2010-10-18 19:17:54", "MM-dd"); //10-18
customDateFormat("2010-10-18 19:17:54", "MM"); //10
customDateFormat("2010-10-18 19:17:54", "dd"); //18
customDateFormat("2010-10-18 19:17:54", "HH:mm:ss"); //19:17:54
customDateFormat("aaaaaa", "HH:mm:ss"); //10:55:08