| 
 | 
'*/----------------------------------------------- -------------- 
'*/Module name: DateFunctions.bas 
'*/Function: string to date and to string conversion function 
'*/Date of establishment: 2016-09-12 
'*/Date of modification: 
'*/Author: 
'*/Contact: Mend@sunseaman.com Http://www.sunseaman.com 
'*/----------------------------------------------- -------------- 
 
Option Explicit 
 
'*/----------------------------------------------- -------------- 
'*/Function name: DateToString 
'*/Function: Convert standard date format to string 
'*/Return value: String continuous string, such as 20160912101227 
'*/Parameter: vStandardDate standard date 
'*/Date of establishment: 2016-09-12 
'*/Date of modification: 
'*/----------------------------------------------- -------------- 
Public Function DateToString(ByRef vStandardDate As Date) As String'SerializeDate 
    Dim strYear As String, strMonth As String, strDay As String 
    Dim strHour As String, strMinute As String, strSecond As String 
     
    strYear = CStr(Year(vStandardDate)) 
    strMonth = CStr(Month(vStandardDate)) 
    strDay = CStr(Day(vStandardDate)) 
    strHour = CStr(Hour(vStandardDate)) 
    strMinute = CStr(Minute(vStandardDate)) 
    strSecond = CStr(Second(vStandardDate)) 
    If CInt(strMonth) <10 Then _ 
        strMonth = "0"&strMonth 
    If CInt(strDay) <10 Then _ 
        strDay = "0"&strDay 
    If CInt(strHour) <10 Then _ 
        strHour = "0"&strHour 
    If CInt(strMinute) <10 Then _ 
        strMinute = "0"&strMinute 
    If CInt(strSecond) <10 Then _ 
        strSecond = "0"&strSecond 
    DateToString = strYear&strMonth&strDay&strHour&strMinute&strSecond 
End Function 
 
'*/----------------------------------------------- -------------- 
'*/Function name: StringToDate 
'*/Function: Convert date in string format to standard date format 
'*/Return value: Date in standard format, such as 2016-09-12 10:12:27 
'*/Parameter: vSerialDate string 
'*/Date of establishment: 2016-09-12 
'*/Date of modification: 
'*/----------------------------------------------- -------------- 
Public Function StringToDate(ByRef vSerialDate As String) As Date'Standardize 
    Dim strDate As String, strYear As String, strMonth As String, strDay As String 
    Dim strHour As String, strMinute As String, strSecond As String 
     
    strYear = Mid(vSerialDate, 1, 4) 
    strMonth = Mid(vSerialDate, 5, 2) 
    strDay = Mid(vSerialDate, 7, 2) 
    strHour = Mid(vSerialDate, 9, 2) 
    strMinute = Mid(vSerialDate, 11, 2) 
    strSecond = Mid(vSerialDate, 13, 2) 
    strDate = strMonth&"/"&strDay&"/"&strYear&""&strHour&":"&strMinute&":"&strSecond 
    StringToDate = CDate(strDate) 
End Function 
 
'*/----------------------------------------------- -------------- 
'*/Function name: CalcDate 
'*/Function: standard date format calculation 
'*/Return value: String start time and end time calculation result 
'*/Parameter: strStartDate start time 
'*/ strEndDate end time 
'*/Date of establishment: 2016-09-12 
'*/Date of modification: 
'*/----------------------------------------------- -------------- 
Public Function CalcDate(ByVal strStartDate As String, ByVal strEndDate As String) As String 
    Dim lngYears As Long, lngDays As Long, lngHours As Long, lngMinutes As Long, lngSeconds As Long 
    Dim dblYears As Double, dblDays As Double, dblHours As Double, dblMinutes As Double, dblSeconds As Double 
    Dim lngTotalSeconds As Long, lngSecondsRemaining As Long 
     
   'strStartDate = StringToDate(strStartDate) 
   'strEndDate = StringToDate(strEndDate) 
     
    If CDate(strStartDate) <CDate(strEndDate) Then 
        lngTotalSeconds = DateDiff("s", CDate(strStartDate), CDate(strEndDate)) 
    Else 
        lngTotalSeconds = DateDiff("s", CDate(strEndDate), CDate(strStartDate)) 
    End If 
    lngSecondsRemaining = lngTotalSeconds 
    lngYears = Fix(lngSecondsRemaining / 31536000) 
    lngSecondsRemaining = lngSecondsRemaining-(lngYears * 31536000) 
     
    lngDays = Fix(lngSecondsRemaining / 86400) 
    lngSecondsRemaining = lngSecondsRemaining-(lngDays * 86400) 
     
    lngHours = Fix(lngSecondsRemaining / 3600) 
    lngSecondsRemaining = lngSecondsRemaining-(lngHours * 3600) 
     
    lngMinutes = Fix(lngSecondsRemaining / 60) 
    lngSecondsRemaining = lngSecondsRemaining-(lngMinutes * 60) 
    lngSeconds = lngSecondsRemaining 
     
'CalcDate = CStr(lngYears)&"year," _ 
'&CStr(lngDays)&"days," _ 
'&CStr(lngHours)&"when," _ 
'&CStr(lngMinutes)&"points," _ 
'&CStr(lngSeconds)&"seconds" 
 
    CalcDate = IIf(lngDays = 0, "", CStr(lngDays)&"天") _ 
           &IIf(lngHours = 0, "", CStr(lngHours)&"时") _ 
           &IIf(lngMinutes = 0, "", CStr(lngMinutes)&"分") _ 
           &IIf(lngSeconds = 0, "", CStr(lngSeconds)&"second") 
 
'dblYears = Round(lngTotalSeconds / 31536000, 4) 
'dblDays = Round(lngTotalSeconds / 86400, 4) 
'dblHours = Round(lngTotalSeconds / 3600, 4) 
'dblMinutes = Round(lngTotalSeconds / 60, 4) 
'dblSeconds = lngTotalSeconds 
'CalcDate = CalcDate&"Decimal time:"&vbNewLine _ 
'&"Year: "&CStr(dblYears)&vbNewLine _ 
'&"Day: "&CStr(dblDays)&vbNewLine _ 
'&"hour: "&CStr(dblHours)&vbNewLine _ 
'&"point: "&CStr(dblMinutes)&vbNewLine _ 
'&"second: "&CStr(dblSeconds) 
End Function 
 
 
'Whether to judge by month 
Public Function DaysInMonth(ByVal Month As Integer, ByVal LeapYear As Boolean) As Integer 
    Select Case Month 
        Case 1, 3, 5, 7, 8, 10, 12 
            DaysInMonth = 31 
        Case 4, 6, 9, 11 
            DaysInMonth = 30 
        Case 2 
            If LeapYear Then 
                DaysInMonth = 29 
            Else 
                DaysInMonth = 28 
        End If 
    End Select 
End Function 
 
'Leap year judgment 
Public Function IsLeapYear(ByVal Year As Integer) As Boolean 
    If Year Mod 4 = 0 Then 
        IsLeapYear = True 
        If Year Mod 100 = 0 And Year Mod 400 <> 0 Then 
            IsLeapYear = False 
        End If 
    End If 
End Function |   
 
 
 
 |