js 获取时间 和推算时间日期的js
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" 的日期格式'; } } function addByTransDate(dateParameter, num) { var translateDate = "", dateString = "", monthString = "", dayString = ""; translateDate = dateParameter.replace("-", "/").replace("-", "/"); ; var newDate = new Date(translateDate); newDate = newDate.valueOf(); newDate = newDate - num * 24 * 60 * 60 * 1000; //备注 如果是往前计算日期则为减号 否则为加号 newDate = new Date(newDate); //如果月份长度少于2,则前加 0 补位 if ((newDate.getMonth() + 1).toString().length == 1) { monthString = 0 + "" + (newDate.getMonth() + 1).toString(); } else { monthString = (newDate.getMonth() + 1).toString(); } //如果天数长度少于2,则前加 0 补位 if (newDate.getDate().toString().length == 1) { dayString = 0 + "" + newDate.getDate().toString(); } else { dayString = newDate.getDate().toString(); } dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString; return dateString; }
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" 的日期格式'; }} function addByTransDate(dateParameter, num) {var translateDate = "", dateString = "", monthString = "", dayString = "";translateDate = dateParameter.replace("-", "/").replace("-", "/"); ;var newDate = new Date(translateDate);newDate = newDate.valueOf();newDate = newDate - num * 24 * 60 * 60 * 1000; //备注 如果是往前计算日期则为减号 否则为加号newDate = new Date(newDate);//如果月份长度少于2,则前加 0 补位if ((newDate.getMonth() + 1).toString().length == 1) {monthString = 0 + "" + (newDate.getMonth() + 1).toString();} else {monthString = (newDate.getMonth() + 1).toString();}//如果天数长度少于2,则前加 0 补位if (newDate.getDate().toString().length == 1) {dayString = 0 + "" + newDate.getDate().toString();} else {dayString = newDate.getDate().toString();}dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString;return dateString;}