| |

VerySource

 Forgot password?
 Register
Search
View: 963|Reply: 5

How to return the number of months and days subtracted from two dates

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-3-23 22:30:01
| Show all posts |Read mode
How to return the number of months and days subtracted from two dates. What I have made is a negative number:
text3.text = text1.text-text2.text (example)

Text3.Text = DateDiff ("m", CDate (Format (Text1.Text, "YYYY-MM-DD")), CDate (Format (Text2.Text, "yyyy-mm-dd")))

If the number of days returned is greater than 15 days counts as a month, less than 15 is not remembered! For example: the return result (9 months and 16 days) is the final result of 10 months. The return (9 months and 14 days) will display 9 results month!

Thank you!
Reply

Use magic Report

1

Threads

21

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 Great Britain

Post time: 2020-7-3 19:15:01
| Show all posts
Text3.Text = DateDiff("d", CDate(Format(Text1.Text, "YYYY-MM-DD")), CDate(Format(Text2.Text, "yyyy-mm-dd")))

This is the number of days, as for the later self look
The most stupid way is to calculate how many months these days
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-3 19:45:01
| Show all posts
'*/----------------------------------------------- --------------
'*/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
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-4 21:45:01
| Show all posts
It should not be so complicated
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-5 12:15:01
| Show all posts
If March 20 to May 10 are you counting January 21? Or February 10?
Reply

Use magic Report

0

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-7 17:15:02
| Show all posts
Compare dates first, then months
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list