Results 1 to 3 of 3

Thread: [RESOLVED] Comparing DateTimes, need help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    99

    Resolved [RESOLVED] Comparing DateTimes, need help

    I would like to compare 2 datetimes; datetime are in format MM/dd/yyyy HH:mm:ss.

    DateTimes should be compared to minutes, excluding seconds.
    E.g. 07/05/2006 09:30:30 and 07/05/2006 09:30:45, in this case dates are the same and times too (because I have excluded seconds in the comparing),
    instead:
    07/05/2006 09:30:30 and 07/05/2006 09:31:30 are different.

    How can I do?

    Thank

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Comparing DateTimes, need help

    DateTimes are NOT in ANY format at all. DateTimes are DateTimes and DateTimes have no format. It is only when you convert the DateTime to a string that format comes into play, because the string can display the date and time in a number different ways.

    Now we've established that, you just need to remove the seconds from your two Dates and compare:
    VB Code:
    1. 'Remove the seconds component.
    2. Dim dt1 As Date = DateTime1.AddSeconds(-DateTime1.Second)
    3. Dim dt2 As Date = DateTime2.AddSeconds(-DateTime2.Second)
    4.  
    5. Select Case Date.Compare(dt1, dt2)
    6.     Case Is < 0
    7.         MessageBox.Show("DateTime1 is before DateTime2")
    8.     Case 0
    9.         MessageBox.Show("DateTime1 is the same as DateTime2")
    10.     Case Is > 0
    11.         MessageBox.Show("DateTime1 is after DateTime2")
    12. End Select
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    99

    Re: [RESOLVED] Comparing DateTimes, need help

    thank a lot

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