因为经常用到根据指定时间查询某日期,但是大佬们整理的都是散装的方法,每次还需要自己二次组装,奥利给!
因为懒,不想百度了!!!所以自己写了一个方法自用,希望也可以帮到大家的忙,如果对你有帮助,麻烦点个赞,谢谢嗷!
Ctrl+C -> Ctrl+V !!!
/*
* @getTheSpecifiedDate
* @param {'String'} date 日期(支持'-'和'/'分隔的日期,精确到s)
* @param {'Number'} num 要格式化的时间 num大于0时,查过去时间,反之查未来时间
* @param {'String'} type 格式化日期类型
* type === 'y' -> 年
* type === 'm' -> 月
* type === 'd' -> 日
* type === 'h' -> 时
* type === 'min' -> 分
* type === 's' -> 秒
* @return {String} 返回传入的合法日期格式
/*
function getTheSpecifiedDate(date, num, type) {
var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, result;
r = new Date();
date = date || (r.getFullYear() + '-' + (r.getMonth()+1) + '-' + r.getDate());
num = num || 1;
type = type || 'd';
if (num === 0) {
return date;
}
a = date.indexOf(' ') === -1 ? date : date.slice(0, date.indexOf(' '));
b = date.indexOf(' ') === -1 ? '' : date.slice(date.indexOf(' ') + 1);
if (/^\d{4}\/\d{1,2}\/\d{1,2}$/g.test(a)) {
a = (a.split('/')).join('-');
d = 0;
} else if (/^\d{1,2}\-\d{1,2}\-\d{4}$/g.test(a)) {
a = a.split('-');
a = a[2] + '-' + a[0] + '-' + a[1];
d = 1;
} else if (/^\d{1,2}\/\d{1,2}\/\d{4}$/g.test(a)) {
a = a.split('/');
a = a[2] + '-' + a[0] + '-' + a[1];
d = 2;
} else {
d = 3;
}
o = b.split(':').concat();
c = b.split(':');
c[2] = typeof c[2] === 'undefined' ? '00' : c[2];
if (((+c[0] > 59) || (+c[0] < 0)) || ((+c[1] > 59) || (+c[1] < 0)) || ((+c[2] > 59) || (+c[2] < 0))) {
console.log('日期非法');
return date;
} else if (!(/((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/ig.test(a))) {
console.log('日期非法');
return date;
}
e = new Date(a + ' ' + b);
f = parseInt(num);
if (type === 'y') {
g = e.getFullYear();
g = g - f;
h = e.getMonth() + 1;
i = e.getDate();
} else if (type === 'm') {
g = e.getFullYear();
h = e.getMonth() + 1;
if (f >= 0) {
h = e.getMonth() + 1;
m = parseInt(f / 12);
n = parseInt(f % 12);
m = n >= h ? m + 1 : m;
if (n >= h) {
h = 12 - (n - h);
} else {
h = h - n;
}
g = g - m;
} else {
f = Math.abs(f);
m = parseInt(f / 12);
n = parseInt(f % 12);
m = (n + h >= 12) ? m + 1 : m;
if (h + n <= 12) {
h = h + n;
} else {
h = (h + n) % 12;
}
g = g + m;
}
i = e.getDate();
} else {
if (type === 'd') {
f = f * 24 * 60 * 60 * 1000;
} else if (type === 'h') {
f = f * 60 * 60 * 1000;
} else if (type === 'min') {
f = f * 60 * 1000;
} else if (type === 's') {
f = f * 1000;
}
f = parseInt(f);
e = new Date(Date.parse(e) - f);
g = e.getFullYear();
h = e.getMonth() + 1;
i = e.getDate();
}
if (g % 4 === 0 && /^(\d{4}\-2\-(30|31))|(\d{4}\-02\-(30|31))$/g.test(g + '-' + h + '-' + i)) {
i = '29';
} else if (g % 4 !== 0 && /^(\d{4}\-2\-(29|30|31))|(\d{4}\-02\-(29|30|31))$/g.test(g + '-' + h + '-' + i)) {
i = '28';
} else if (/^(\d{4}\-(4|6|9|11)\-31)|(\d{4}\-(04|06|09|11)\-31)$/g.test(g + '-' + h + '-' + i)) {
i = '30';
}
j = e.getHours() < 10 ? '0' + e.getHours() : e.getHours();
k = e.getMinutes() < 10 ? '0' + e.getMinutes() : e.getMinutes();
l = e.getSeconds() < 10 ? '0' + e.getSeconds() : e.getSeconds();
switch (d) {
case 0:
if (!b) {
return g + '/' + h + '/' + i;
} else if (o.length === 2) {
return g + '/' + h + '/' + i + ' ' + j + ':' + k;
}
return g + '/' + h + '/' + i + ' ' + j + ':' + k + ':' + l;
case 1:
if (!b) {
return h + '-' + i + '-' + g;
} else if (o.length === 2) {
return h + '-' + i + '-' + g + ' ' + j + ':' + k;
}
return h + '-' + i + '-' + g + ' ' + j + ':' + k + ':' + l;
case 2:
if (!b) {
return h + '/' + i + '/' + g;
} else if (o.length === 2) {
return h + '/' + i + '/' + g + ' ' + j + ':' + k;
}
return h + '/' + i + '/' + g + ' ' + j + ':' + k + ':' + l;
case 3:
if (!b) {
return g + '-' + h + '-' + i;
} else if (o.length === 2) {
return g + '-' + h + '-' + i + ' ' + j + ':' + k;
}
return g + '-' + h + '-' + i + ' ' + j + ':' + k + ':' + l;
default:
return '请您传入 "yyyy-mm-dd hh:mm:ss"、 "yyyy/mm/dd hh:mm:ss"、 "yyyy-mm-dd hh:mm"、 "yyyy/mm/dd hh:mm"、"mm-dd-yyyy hh:mm:ss"、 "mm/dd/yyyy hh:mm:ss"、 "mm-dd-yyyy hh:mm"、 "mm-dd-yyyy hh:mm" 的日期格式';
}
}
demo:
// console.log(formatterDate('2019/05/31', -20, 'y'));
// console.log(formatterDate('2019/05/31', 20, 'y'));
// console.log(formatterDate('2019-05-31', -20, 'y'));
// console.log(formatterDate('2019-05-31', 20, 'y'));
// console.log(formatterDate('5/31/2019', -20, 'y'));
// console.log(formatterDate('5/31/2019', 20, 'y'));
// console.log(formatterDate('5-31-2019', -20, 'y'));
// console.log(formatterDate('5-31-2019', 20, 'y'));
// console.log(formatterDate('2019/05/31 12:12', -20, 's'));
// console.log(formatterDate('2019/05/31 12:12', 20, 'm'));
// console.log(formatterDate('2019-05-31 12:12:12', -20, 'm'));
// console.log(formatterDate('2019-05-31 12:12:12', 20, 'y'));
// console.log(formatterDate('5/31/2019 12:12:12', -20, 'y'));
// console.log(formatterDate('5/31/2019 12:12:12', 20, 'y'));
// console.log(formatterDate('5-31-2019 12:12:12', -20, 'y'));
// console.log(formatterDate('5-31-2019 12:12:12', 20, 'y'));
console.log(formatterDate());