util.js
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
function formatDateString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var newDate = this.formatTime(date)
return newDate
}
function yearFormString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var year = date.getFullYear()
return year
}
function monthFormString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var month = date.getMonth() + 1
return month
}
function dayFormString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var day = date.getDate()
return day
}
//获取当前时间,格式YYYY-MM-DD
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
/**
* 检验18位身份证号码(15位号码可以只检测生日是否正确即可)
* @author wolfchen
* @param idCardNo 18为的身份证号码
* @return Boolean 是否合法
**/
function isIDCardNum(idCardNo) {
var regID_18 = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
var regID_15 = /^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$/;
var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//加权因子
var arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//校验码
if (regID_15.test(idCardNo)) {
return true;
} else if (regID_18.test(idCardNo)) {
var sum = 0, idx;
for (var i = 0; i < idCardNo.length - 1; i++) {
// 对前17位数字与权值乘积求和
sum += parseInt(idCardNo.substr(i, 1), 10) * arrExp[i];
}
// 计算模(固定算法)
idx = sum % 11;
// 检验第18为是否与校验码相等
return arrValid[idx] == idCardNo.substr(17, 1).toUpperCase();
} else {
return false;
}
}
module.exports = {
formatTime: formatTime,
formatDateString: formatDateString,
yearFormString: yearFormString,
monthFormString: monthFormString,
dayFormString: dayFormString,
isIDCardNum: isIDCardNum
}