/*取日期多少天后的日期*/
function getDate(date, changeDay) {
var year = date.getFullYear() // 年
var month = date.getMonth() // 月
var day = date.getDate();
return dateFormat('YYYY-mm-dd', new Date(year, month, day + changeDay))
}
function getMonth(date, changeMonth) {
var year = date.getFullYear() // 年
var month = date.getMonth() // 月
var day = date.getDate();
return dateFormat('YYYY-mm-dd', new Date(year, month + changeMonth, day))
}
/*根据日期计算年龄*/
function calcAge(birthday,endDate){
var reg = /\d{4}-\d{2}-\d{2}/;
var age = '';
if(birthday && endDate && reg.test(birthday) && reg.test(endDate)){
//日期相差的月份
var month = (endDate.substr(0,4) * 12 + Number(endDate.substr(5,2))) - (birthday.substr(0,4) * 12 + Number(birthday.substr(5,2)));
//月份相同,结束日期天比出生日期天早,不满足月
if(month % 12 === 0 && birthday.substr(8,2) > endDate.substr(8,2)){
month -= 1;
}
//不是12月的倍数,结束日期比出生日期早,不满足月
var age_m = '';
age_m = month % 12;
if(month % 12 > 0 && birthday.substr(8,2) > endDate.substr(8,2)){
age_m -= 1;
}
age = Math.floor(month / 12) +'周岁' + (age_m > 0 ? age_m + '月' :'')
}
if(!age){
age = '参数不正确'
}
return age;
}
//日期格式化
//自己实现一个padStart(),实现给字符串‘hello’前补0,考虑性能。
String.prototype.zpadStart = function (targetLength, padString) {
var string = this;
while (string.length < targetLength) {
string = padString + string
}
return string
}
//格式化时间 dateFormat("YYYY-mm-dd HH:MM", new Date()) date为时间对象,new Date()传入时间戳时需要为数字类型
function dateFormat(fmt, date) {
var ret;
var opt = {
"Y+": date.getFullYear().toString(), // 年
"m+": (date.getMonth() + 1).toString(), // 月
"d+": date.getDate().toString(), // 日
"H+": date.getHours().toString(), // 时
"M+": date.getMinutes().toString(), // 分
"S+": date.getSeconds().toString() // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (var k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
//fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].zpadStart(ret[1].length, "0")))//兼容ES5写法,格式化显示为1位以上时补0
};
};
return fmt;
}
//判断字符串是否格式正确及日期合法性
function isDate(str) {
if(!str)return false;
var result = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (result == null){
return false;
}
var d = new Date(result[1], result[3] - 1, result[4]);
return (d.getFullYear() == result[1] && d.getMonth() + 1 == result[3] && d.getDate() == result[4]);
}
//判断是否是闰年
function isLeapYear(year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 100 == 0) {
return true;
} else {
return false;
}
}
//获取指定月份的天数
function getDayByMonth(year, month) {
//1 2 3 4 5 6 7 8 9 10 11 12
var days = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (!isLeapYear(year)) { //平年28天
days[1] = 28;
}
return days[month];
}
//比较两个日期大小 参数格式2019-07-01 时间1 大于等于 时间2 则返回1
function dateCompare(date1, date2) {
if (!date1 || !date2) {
return 0;
}
date1 = $.trim(date1).replace(/\-/gi, "/");//iOS不支持-的日期格式比较
date2 = $.trim(date2).replace(/\-/gi, "/");
var time1 = new Date(date1).getTime();
var time2 = new Date(date2).getTime();
if (time1 >= time2) {
return 1;
} else {
return 2;
}
}
//计算一年总共多少周
function getNumOfWeek(year) {
var firstDay = new Date(year + "/01/01").getDay(); //本年第一天是星期几 星期天是0
var variate = firstDay === 1 ? 0 : 1;//判断当前年第一天是否是周一
var nowTotalDays = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365;
var totalWeek = Math.ceil(nowTotalDays / 7.0);
if(variate === 1 ){//本年第一天不是星期一,示例2019年1月1号为周二,周二至周日(6号)归于2018年最后一周,本年计算周次时间需减去周二至周日共计6天,计算 = 8 - 2(1号周几);
totalWeek = Math.ceil((nowTotalDays - (8 - firstDay)) / 7.0);
}
return totalWeek;
}
/**
* 获取某年的某天是第几周
* @param {Number} y
* @param {Number} m
* @param {Number} d
* @returns {Number}
*/
function theWeek(date) {
var now = new Date(date),
year = now.getFullYear(),
month = now.getMonth(),
days = now.getDate();
//计算当天所在年份的第多少天
var nowTotalDays = 0;
for (var i = 0; i < month; i++) {
nowTotalDays += getMonthDays(year, i);
}
nowTotalDays += days;//今年第一天到当天的总天数
var firstDay = new Date(year +"/01/01").getDay();//console.log("本年第一天是星期:",firstDay);
var variate = firstDay === 1 ? 0 : 1;//判断当前年第一天是否是周一
var nowWeek = Math.ceil(nowTotalDays / 7.0);
if(variate === 1 ){//本年第一天不是星期一
nowWeek = Math.ceil((nowTotalDays - (8 - firstDay)) / 7.0) <= 0 ? getNumOfWeek(year - 1):Math.ceil((nowTotalDays - (8 - firstDay)) / 7.0);
}
return nowWeek;
}
//获取当前日期所在周周一的日期 参数:'2019-07-26'
function theWeekMonday(date) {
if (typeof date == "string") {
date = date.replace(/-/g, "/");
}
var now = new Date(date);
var day = now.getDay();
day = day == 0 ? 6 : day - 1;
var monday = now.getTime() - day * 24 * 3600 * 1000;
now = new Date(monday);
return myformatter(now);
}
//日期格式化 参数:Date时间对象
function myformatter(date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
//当天日期格式化
function nowDateFormatter() {
var date = new Date();
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
//获取某天日期N天后的日期
function theDayByFuture(date, afterDay) {
if (typeof date == "string") {
date = date.replace(/-/g, "/");
}
var now = new Date(date);
var monday = now.getTime() + afterDay* 24 * 3600 * 1000;
now = new Date(monday);
var y = now.getFullYear();
var m = now.getMonth() + 1;
var d = now.getDate();
return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
//这个方法将取得某年(year)第几周(weeks)的星期几(weekDay)的日期
//例 参数'2019','1','1' 2019年第一个周一日期为2019-01-07
function getXDate(year, weeks, weekDay) {
var date = new Date(year, 0, 1);
if (date.getDay() == 1) {
weeks = weeks - 1;
}
var time = date.getTime();
time += (weeks) * 7 * 24 * 3600000;
date.setTime(time);
return getNextDate(date, weekDay);
}
//这个方法将取得 某日期(nowDate) 所在周的星期几(weekDay)的日期
//例 参数:'2019-07-26','1' 返回这个日期所在周周一日期是'2019-07-22'
function getNextDate(date, day) {
//0是星期日,1是星期一,...
if (typeof date == "string") {
date = date.replace(/-/g, "/");
}
var now = new Date(date);
var wday = now.getDay();
wday = wday == 0 ? 7 : wday;
var time = now.getTime();
var sub = day - wday;
time += sub * 24 * 3600000;
now.setTime(time);
return myformatter(now);
}
/** 校验身份证方法1(推荐) **/
function validateIdCard(idCard){
//15位和18位身份证号码的正则表达式
var regIdCard=/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
//如果通过该验证,说明身份证格式正确,但准确性还需计算
if(regIdCard.test(idCard)){
if(idCard.length==18){
var idCardWi=new Array( 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ); //将前17位加权因子保存在数组里
var idCardY=new Array( 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ); //这是除以11后,可能产生的11位余数、验证码,也保存成数组
var idCardWiSum=0; //用来保存前17位各自乖以加权因子后的总和
for(var i=0;i<17;i++){
idCardWiSum+=idCard.substring(i,i+1)*idCardWi[i];
}
var idCardMod=idCardWiSum%11;//计算出校验码所在数组的位置
var idCardLast=idCard.substring(17);//得到最后一位身份证号码
//如果等于2,则说明校验码是10,身份证号码最后一位应该是X
if(idCardMod==2){
if(idCardLast=="X"||idCardLast=="x"){
//console.log("身份证号码正确!");
}else{
layer.msg("身份证号码信息有误!");
return false;
}
}else{
//用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
if(idCardLast==idCardY[idCardMod]){
//console.log("身份证号码正确!");
}else{
layer.msg("身份证号码信息有误!");
return false;
}
}
}
}else{
layer.msg("身份证号码信息有误!");
return false;
}
return true;
}
/** 校验身份证方法2 **/
function IdentityCodeValid(code) {
var city = {
11 : "北京",
12 : "天津",
13 : "河北",
14 : "山西",
15 : "内蒙古",
21 : "辽宁",
22 : "吉林",
23 : "黑龙江 ",
31 : "上海",
32 : "江苏",
33 : "浙江",
34 : "安徽",
35 : "福建",
36 : "江西",
37 : "山东",
41 : "河南",
42 : "湖北 ",
43 : "湖南",
44 : "广东",
45 : "广西",
46 : "海南",
50 : "重庆",
51 : "四川",
52 : "贵州",
53 : "云南",
54 : "西藏 ",
61 : "陕西",
62 : "甘肃",
63 : "青海",
64 : "宁夏",
65 : "新疆",
71 : "台湾",
81 : "香港",
82 : "澳门",
91 : "国外 "
};
var tip = "";
var pass = true;
if (!code|| !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
tip = "身份证号格式错误";
pass = false;
}
else if (!city[code.substr(0, 2)]) {
tip = "身份证地址编码错误";
pass = false;
} else {
//18位身份证需要验证最后一位校验位
if (code.length == 18) {
code = code.split('');
//∑(ai×Wi)(mod 11)
//加权因子
var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5,8, 4, 2 ];
//校验位
var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
var sum = 0;
var ai = 0;
var wi = 0;
for ( var i = 0; i < 17; i++) {
ai = code[i];
wi = factor[i];
sum += ai * wi;
}
var last = parity[sum % 11];
if (parity[sum % 11] != code[17]) {
tip = "身份证校验位错误";
pass = false;
}
}
}
if (!pass)
layer.alert(tip);
return pass;
}
//身份证号码判断性别 1=>男 2=>女
function getSexByCardid(cardid) {
if (cardid.length == 15) {
if (parseInt(cardid.charAt(14) / 2) * 2 != cardid.charAt(14)) {
return "1";
} else {
return "2";
}
} else if (cardid.length == 18) {
if (parseInt(cardid.charAt(16) / 2) * 2 != cardid.charAt(16)) {
return "1";
} else {
return "2";
}
}
}
//通过身份证计算年龄 返回例 [20,2,20] => 20岁2个月20天
function getAgeByCardid(cardid, currentDate) {
var birthYear, birthMonth, birthDay;
if (cardid.length == 15) {
birthYear = "19" + cardid.substr(6, 2);
birthMonth = cardid.substr(8, 2);
birthDay = cardid.substr(10, 2);
} else if (cardid.length == 18) {
birthYear = cardid.substr(6, 4);
birthMonth = cardid.substr(10, 2);
birthDay = cardid.substr(12, 2);
} else {
alert("身份证长度不合法。");
return false;
}
endCalcYear = currentDate.substr(0, 4);
endCalcMonth = Number(currentDate.substr(5, 2));
endCalcDay = Number(currentDate.substr(8, 2));
return getAge(birthYear, birthMonth, birthDay, endCalcYear, endCalcMonth, endCalcDay);
}
/* birthYear 出生年
* birthMonth 出生月 从1开始(1-12)
* birthDay 出生天
* endCalcYear 计算截止日期年
* endCalcMonth 计算截止日期月(月份从1开始,即1-12)
* endCalcDay 计算截止日期天
* 返回例 [20,2,20] => 20岁2个月20天
*/
function getAge(birthYear, birthMonth, birthDay, endCalcYear, endCalcMonth, endCalcDay) {
var bDate = new Date(); //生日
bDate.setFullYear(birthYear, birthMonth - 1, birthDay); //月份从0开始
var eDate = new Date(); //计算截止日期
eDate.setFullYear(endCalcYear, endCalcMonth - 1, endCalcDay); //月份从0开始
var day = eDate.getDate() - bDate.getDate(); //天
var month = eDate.getMonth() - bDate.getMonth(); //月
var year = eDate.getFullYear() - bDate.getFullYear(); //年
if (day < 0) {
month -= 1;
var tempMonth = eDate.getMonth() - 1; //获取截至计算的月减去1
var tempYear = eDate.getFullYear(); //获取截至计算的年
//如果月份是1(javascript:0)月份,那么年份就减1,月份设置成11,也就是上一年度的12月份
if (tempMonth < 0) {
tempYear -= 1;
tempMonth = 11;
}
var addDay = getDayByMonth(tempYear, tempMonth); //获取上一月的天数。
day = day + addDay;
}
if (month < 0) {
month = (month + 12) % 12;
year--;
}
return new Array(year, month, day);
}
//博学谷身份证验证(包含港澳台)
var aCity = {
11: "北京",
12: "天津",
13: "河北",
14: "山西",
15: "内蒙古",
21: "辽宁",
22: "吉林",
23: "黑龙江",
31: "上海",
32: "江苏",
33: "浙江",
34: "安徽",
35: "福建",
36: "江西",
37: "山东",
41: "河南",
42: "湖北",
43: "湖南",
44: "广东",
45: "广西",
46: "海南",
50: "重庆",
51: "四川",
52: "贵州",
53: "云南",
54: "西藏",
61: "陕西",
62: "甘肃",
63: "青海",
64: "宁夏",
65: "新疆",
71: "台湾",
81: "香港",
82: "澳门",
91: "国外"
}
, taiwaiPatt = /^[A-KM-QT-XZ]{1}[0-9]{9}$/
, HKPatt = /^[A-Z][0-9]{5,8}[0-9A]$/
, aomenPatt = /^[157][0-9]{6}[0-9]$/;
function isCardID(t) {
if (taiwaiPatt.test(t) || HKPatt.test(t) || aomenPatt.test(t))
return !0;
if (taiwaiPatt.test(t) || HKPatt.test(t) || aomenPatt.test(t))
return "身份证号格式错误";
var e = 0;
if (!/^\d{17}(\d|x)$/i.test(t))
return "身份证号格式错误";
if (t = t.replace(/x$/i, "a"),
null == aCity[parseInt(t.substr(0, 2))])
return "身份证号格式错误";
sBirthday = t.substr(6, 4) + "-" + Number(t.substr(10, 2)) + "-" + Number(t.substr(12, 2));
var i = new Date(sBirthday.replace(/-/g, "/"));
if (sBirthday != i.getFullYear() + "-" + (i.getMonth() + 1) + "-" + i.getDate())
return "身份证号格式错误";
for (var s = 17; s >= 0; s--)
e += Math.pow(2, s) % 11 * parseInt(t.charAt(17 - s), 11);
return e % 11 == 1 || "身份证号格式错误"
}
//通过身份证号码前12位和后4位推出有效的身份证号码
var id = "";
var id1 = '5101081976';
var id3 = '0314';
var total = 0;
for (var i = 0; i < 2; i++) {//月十位
total++;
for (var i1 = 0; i1 < 3; i1++) {//月个位
if (i === 1 && i1 > 2) {
continue;
}
total++;
for (var i2 = 0; i2 < 4; i2++) {//日十位
id = id1 + i + i1 + i2 + '0' + id3;
if(isCardID(id) === true){
console.log(id + ' --身份证号码正确--');
};
total++;
for (var i3 = 1; i3 < 10; i3++) {//日个位
if (i2 >= 3 && i3 > 2) {
continue;
}
id = id1 + i + i1 + i2 + i3 + id3;
if(isCardID(id) === true){
console.log(id + ' --身份证号码正确--');
};
total++;
}
}
}
}
console.log(total)//4位数的组合可能性为11110次,加上月份条件后执行总次数为206次,可以得到12个左右有效的身份证号码