Results 1 to 3 of 3

Thread: [RESOLVED] Date Differences

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] Date Differences

    OK, firstly, I know about DateDiff and TimeSpan...

    I am doing a project. I save a date in the registry. Now, I have to test this date against today's date and calculate the differences in minutes, hours, days, months, weeks and years.

    I save the date in the registry like this:
    Code:
     Application.UserAppDataRegistry.SetValue("Date", [Date])
    I then made a sub to calculate the differences:
    Code:
     Private Sub Calculate()
            Try
                Dim DateGen As Date
    
                DateGen = CType([Date], Date)
    
                Select Case TimePeriod
    
                    Case "Every Minute" 
                        If DateDiff(DateInterval.Minute, DateGen, Now) >= 1 Then
                            RandomAlpha()
                        End If
                    Case "Every x Minute(s)" '
                        If DateDiff(DateInterval.Minute, Now, DateGen) >= Duration Then
                            RandomAlpha()
                        End If
                    Case "Every Hour"
                        If DateDiff(DateInterval.Hour, DateGen, Now) >= 1 Then
                            RandomAlpha()
                        End If
                    Case "Every x Hour(s)"
                        If DateDiff(DateInterval.Hour, DateGen, Now) >= Duration Then
                            RandomAlpha()
                        End If
                    Case "Every Day"
                        If DateDiff(DateInterval.Day, DateGen, Now) >= 1 Then
                            RandomAlpha()
                        End If
                    Case "Every x Day(s)"
                        If DateDiff(DateInterval.Day, DateGen, Now) >= Duration Then
                            RandomAlpha()
                        End If
                    Case "Every Week"
                        If DateDiff(DateInterval.Day, DateGen, Now) >= 7 Then
                            RandomAlpha()
                        End If
                    Case "Every x Week(s)"
                        If DateDiff(DateInterval.Day, DateGen, Now) >= (Duration * 7) Then
                            RandomAlpha()
                        End If
                    Case "Every Month"
                        If DateDiff(DateInterval.Month, DateGen, Now) >= 1 Then
                            RandomAlpha()
                        End If
                    Case "Every x Month(s)"
                        If DateDiff(DateInterval.Month, DateGen, Now) >= Duration Then
                            RandomAlpha()
                        End If
                    Case "Every Year"
                        If DateDiff(DateInterval.Year, DateGen, Now) >= 1 Then
                            RandomAlpha()
                        End If
                    Case "Every x Year(s)"
                        If DateDiff(DateInterval.Year, DateGen, Now) >= Duration Then
                            RandomAlpha()
                        End If
                End Select
    
            Catch eg As Exception
                MessageBox.Show(eg.Message())
                tmrCalcGen.Enabled = False
    
            End Try
        End Sub
    As you can see, based on the difference between these dates ( today's date and the registry date ), a sub gets called and the program continues.

    Unfortunately, the minutes and hours never calls the RandomAlpha sub, because, I think it gets the wrong answer.

    How can I calculate the difference in minutes, hours etc.????

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Date Differences

    The DateDiff function will return minus values. In your code you have used
    Code:
    If DateDiff(DateInterval.Minute, DateGen, Now) >= 1 Then
    If the Now value is Greater than it will return + values. If the Now Value is Lesser tha DateGen it will return - values. You need to change your code Accordingly.

    Code:
    DateDiff(DateInterval.Minute, Date1, Date2)
    If the Date2 is Greater it will return +
    If the Date2 is Lesser it will return _ values
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Date Differences

    Ah, stupid me!!!!
    Thank you so much!

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