-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.go
42 lines (39 loc) · 983 Bytes
/
id.go
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
package function
import (
"errors"
"strings"
"time"
)
// IdCardBirthHandle 身份证出生日期处理,统一格式:[2020年1月1日]
func IdCardBirthHandle(birth string, layouts ...string) (time.Time, error) {
layout := "2006年1月2日"
if len(layouts) > 0 {
layout = layouts[0]
}
return time.Parse(layout, birth)
}
// IdCardDeadlineHandle 身份证有效期处理,统一格式:[2018.08.01-2028.08.01]
func IdCardDeadlineHandle(date string, layouts ...string) (starTime, endTime time.Time, err error) {
layout := "2006.01.02"
if len(layouts) > 0 {
layout = layouts[0]
}
arr := strings.Split(date, "-")
if len(arr) != 2 {
err = errors.New("身份证有效期限格式不正确-1")
return
}
starTime, err = time.Parse(layout, arr[0])
if err != nil {
return
}
endTime, err = time.Parse(layout, arr[1])
if err != nil {
return
}
if endTime.Unix() <= starTime.Unix() {
err = errors.New("身份证有效期限格式不正确-2")
return
}
return
}