Monday, April 11, 2011

VB.NET: Compare just the date part and not the time of two Dates

Hi,

I want to compare just the date part (and Not the time) of two VB.NET Date objects. Is there a way to do that?

Hoping to get some answers.

/Ylva

From stackoverflow
  • Compare the DateTime.Date properties.

  • Just take the date part of each via the Date property and compare the two:

    date1.Date.CompareTo(date2.Date)
    

    Or:

    If date1.Date < date2.Date Then
    
    Konrad Rudolph : You assume correctly.
    Jon Skeet : I'll remove the text about it then. Just for anyone following along, Konrad's comment made sense at the time :)
    JaredPar : +1, added a minor bit to the VB if/then syntax.
    Jon Skeet : Thanks Jared. When I answer VB questions there's almost always a bit of syntactic guesswork involved :)
  • You could also use TimeSpan

    Dim ts As TimeSpan
    ts = dt1 - dt2
    

    ts.Days will now have the difference of the two dates as whole days.

0 comments:

Post a Comment