Code
Option Explicit
'身份证类
'通过身份证编号获取性别和出生日期
Private ID_ As String
Private Const IDCARD_LENGTH_DESCRIPTION As String = "身份证编号必须为15位或者18位"
'身份证编号
Public Property Get ID() As String
Assert Len(ID_) = 15 Or Len(ID_) = 18, Me, "ID", IDCARD_LENGTH_DESCRIPTION
ID = ID_
End Property
Public Property Let ID(ByVal sID As String)
Assert Len(sID) = 15 Or Len(sID) = 18, Me, "ID", IDCARD_LENGTH_DESCRIPTION
ID_ = sID
End Property
'身份证号长度
Public Property Get IDLength() As Integer
IDLength = Len(ID_)
End Property
'性别
Public Property Get SEX() As String
Dim sTemp As String
Assert IDLength = 15 Or IDLength = 18, Me, "SEX", IDCARD_LENGTH_DESCRIPTION
Select Case IDLength
Case 15
sTemp = Mid(ID_, 13, 3)
Case 18
sTemp = Mid(ID_, 3, 15)
End Select
If CInt(sTemp) Mod 2 = 0 Then
SEX = "女"
Else
SEX = "男"
End If
End Property
'出生日期
Public Property Get Birthday() As String
Dim sTemp As String
Assert IDLength = 15 Or IDLength = 18, Me, "Birthday", IDCARD_LENGTH_DESCRIPTION
Select Case IDLength
Case 15
sTemp = Mid(ID_, 7, 6)
sTemp = "19" & sTemp
Case 18
sTemp = Mid(ID_, 7, 8)
End Select
Birthday = Mid(sTemp, 1, 4) & "/" & Mid(sTemp, 5, 2) & "/" & Mid(sTemp, 7, 2)
End Property
Option Explicit
'身份证类
'通过身份证编号获取性别和出生日期
Private ID_ As String
Private Const IDCARD_LENGTH_DESCRIPTION As String = "身份证编号必须为15位或者18位"
'身份证编号
Public Property Get ID() As String
Assert Len(ID_) = 15 Or Len(ID_) = 18, Me, "ID", IDCARD_LENGTH_DESCRIPTION
ID = ID_
End Property
Public Property Let ID(ByVal sID As String)
Assert Len(sID) = 15 Or Len(sID) = 18, Me, "ID", IDCARD_LENGTH_DESCRIPTION
ID_ = sID
End Property
'身份证号长度
Public Property Get IDLength() As Integer
IDLength = Len(ID_)
End Property
'性别
Public Property Get SEX() As String
Dim sTemp As String
Assert IDLength = 15 Or IDLength = 18, Me, "SEX", IDCARD_LENGTH_DESCRIPTION
Select Case IDLength
Case 15
sTemp = Mid(ID_, 13, 3)
Case 18
sTemp = Mid(ID_, 3, 15)
End Select
If CInt(sTemp) Mod 2 = 0 Then
SEX = "女"
Else
SEX = "男"
End If
End Property
'出生日期
Public Property Get Birthday() As String
Dim sTemp As String
Assert IDLength = 15 Or IDLength = 18, Me, "Birthday", IDCARD_LENGTH_DESCRIPTION
Select Case IDLength
Case 15
sTemp = Mid(ID_, 7, 6)
sTemp = "19" & sTemp
Case 18
sTemp = Mid(ID_, 7, 8)
End Select
Birthday = Mid(sTemp, 1, 4) & "/" & Mid(sTemp, 5, 2) & "/" & Mid(sTemp, 7, 2)
End Property