Javascript常用工具类封装
- 获取当前时间
yyyy-MM-dd HH:mm:ss
或yyyy-MM-dd
常用版
function getFormatDate(isCompleteDate = true) {
// 获取带时分秒的,不带的记得加false
{
let t = new Date(),
y = t.getFullYear(),
m = t.getMonth() + 1,
a = t.getDate(),
h = t.getHours(),
min = t.getMinutes(),
s = t.getSeconds(),
str = (1 <= m && m <= 9 && (m = "0" + m),
0 <= a && a <= 9 && (a = "0" + a),
0 <= h && h <= 9 && (h = "0" + h),
0 <= min && min <= 9 && (min = "0" + min),
0 <= s && s <= 9 && (s = "0" + s),
y + "-" + m + "-" + a);
if (isCompleteDate) {
str = str + " " + h + ":" + min + ":" + s;
}
return str;
}
}
精简版
function getFormatDate(isCompleteDate = true) {
{
let t = new Date(),
y = t.getFullYear(),
m = t.getMonth() + 1,
d = t.getDate(),
h = t.getHours(),
min = t.getMinutes(),
s = t.getSeconds();
return 1 <= m && m <= 9 && (m = "0" + m),
0 <= d && d <= 9 && (d = "0" + d),
0 <= h && h <= 9 && (h = "0" + h),
0 <= min && min <= 9 && (min = "0" + min),
0 <= s && s <= 9 && (s = "0" + s), isCompleteDate ?
(y + "-" + m + "-" + d + ' ' + h + ':' + min + ':' + s) : y + "-" + m + "-" + d;
}
}