Results 1 to 5 of 5

Thread: DateDiff

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    I can't take it anymore. I've been doing it since forever and I guess it's time to give up. What I am trying to do is to calculate difference between to dates in Years, Months, Days, Hours, Minutes and Seconds. I know how to handle y,m,h,minutes and s using DateDiff, but my problem begins when I'm trying to calculate Days. with my function I'm getting 38 35, even 40 days (maximum should be 31 or 30 I'm not sure about it). Does anyone have any ideas how to write function/procedure to calculate this? I would really appreciate any help. If you need my Source Code feel free to E-mail me or just reply to this message saying so.
    Thanks in advance. I know you can do this.

    ------------------
    Visual Basic Programmer
    -----------------
    PolComSoft
    You will hear a lot about it.


  2. #2
    Junior Member
    Join Date
    Oct 1999
    Posts
    31

    Post

    When you use "Days" as the interval in DateDiff, it gives the number of physical days between the two dates. Thats the reason you are getting more than 31/30 days. Can you post the source code of what you have done till now.

    Thanks.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    Here is my code:
    Code:
        ISeconds = DateDiff("s", Now, Data)
        IMonths = DateDiff("m", Now, Data)
        IYears = DateDiff("yyyy", Now, Data)
        IDays = DateDiff("d", Now, Data)
        
        IMinuty = ISeconds \ 60
        ISeconds = ISeconds - IMinuty * 60
        IHours = IMinuty \ 60
        IMinuty = IMinuty - IHours * 60
    
        If Hour(Format(Data, "Short Time")) >= Hour(Format(Now, "SHort Time")) Then
            IHours = Hour(Format(Data, "Short time")) - Hour(Format(Now, "Short Time")) - 1
        Else
            IHours = (24 - (24 - Hour(Format(Data, "SHort Time")))) + (24 - Hour(Format(Now, "Short time"))) - 1
        End If
        If IMonths > 0 Then
            IMonths = IMonths - 1
        End If
        If IYears > 0 Then
            IYears = IYears - 1
        End If
        
        NewMonth = Month(Data)
        
        If Val(NewMonth) = 0 Then
            NewMonth = 12
        End If
        
        dateNow = Format(Now, "mm/dd/yyyy hh:nn:ss AMPM")
        NewDate= dateNow
        If NewMonth < 10 Then
            Mid(NowaData, 1, 2) = "0" & NewMonth
        Else
            Mid(NowaData, 1, 2) = NewMonth
        End If
    
        On Error Resume Next
        nData = DateValue(NowaData)
        'I know that next part with Days doesn't work properly    
        If Year(Data) <> Year(Now) Then
            If IDaysWMiesiacu(nData) > Day(Teraz) Then
                IDays = (IDaysWMiesiacu(nData) - Day(Teraz)) + Day(Data) - 1
            Else
                IDays = Day(Data) - 1
            End If
        ElseIf Month(Now) <> Month(Data) Then
            IDays = (DaysInMonth(nData) - Day(nData)) + Day(Data) - 1
        ElseIf Month(Now) = Month(Data) Then
            IDays = Day(Data) - Day(nData) - 1
        End If
        
        IMonths = IMonths - IYears * 12
        If IMonths >= 12 Then
            IYears = IYears + IMonths \ 12
            IMonths = IMonths - IMonths \ 12
        End If
    ------------------
    Visual Basic Programmer
    -----------------
    PolComSoft
    You will hear a lot about it.

    [This message has been edited by QWERTY (edited 11-04-1999).]

  4. #4
    Lively Member
    Join Date
    Jun 1999
    Location
    Ireland
    Posts
    96

    Post

    Just Place 1 Timer on the form, with an interval of 1000 and this should work.

    'Date string is given in
    'Months/Days Hours:Minutes:Seconds.

    Private Function TimeRemaining(DestinationDate As String) As String
    Dim tmpTime As Long, tmpMonth As Integer, tmpDay As Integer, tmpHour As Integer, tmpMin As Integer, tmpSec As Integer
    Dim varMonth(12) As Integer, n As Integer

    'Setup Month Lengths
    For n = 1 To 12: varMonth(n) = 31: Next n
    If Year(Now) Mod 4 = Int(Year(Now) Mod 4) Then
    varMonth(2) = 29
    Else
    varMonth(2) = 28
    End If
    varMonth(4) = 30: varMonth(6) = 30: varMonth(9) = 30: varMonth(11) = 30

    'Calculate Differences
    tmpTime = DateDiff("s", Now, DestinationDate)
    tmpDay = Int(tmpTime / 86400)
    tmpHour = Int(tmpTime / 3600) Mod 24
    tmpMin = Int(tmpTime / 60) Mod 60
    tmpSec = tmpTime Mod 60

    Do While tmpDay > varMonth(Month(Now))
    If tmpDay > varMonth(Month(Now)) Then
    tmpDay = tmpDay - varMonth(((Month(Now) + tmpMonth - 1) Mod 12) + 1)
    tmpMonth = tmpMonth + 1
    End If
    Loop

    'Return the Time Remaining String
    TimeRemaining = tmpMonth & "/" & tmpDay & " " & tmpHour & ":" & tmpMin & ":" & tmpSec
    End Function

    Private Sub Timer1_Timer()
    Me.Caption = TimeRemaining("99/12/06 23:59:59")
    End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    Thanks!
    I'll check it

    ------------------
    Visual Basic Programmer
    -----------------
    PolComSoft
    You will hear a lot about it.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width