JDYoder Posted July 12, 2005 Posted July 12, 2005 I'm trying to figure out the .NET equivalent of VB6's DateDiff function, but so far the best I could find is the TimeSpan object. However, it appears quite limited since it only goes up to "days", which isn't enough for my purposes. I need to be able to find the difference in months or years between two dates, like DateDiff was capable of doing. What's the .NET equivalent do this? For instance, the old VB6 lines below would give me "63" and "5"... MsgBox DateDiff("m", "April 1973", "July 1978") MsgBox DateDiff("yyyy", "April 1973", "January 1978") How can I do this in .NET? Quote
Afraits Posted July 12, 2005 Posted July 12, 2005 Use the DateTime subtract method eg DateDiffVar=mydatevar.Subtract(EarlierDateVar) This will subtract EarlierDateVar from mydatevar and leave the difference in DateDiffVar. You can then use the .ToString(format) or .Month .Day or .Year methods to give you the values you are after. Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
JDYoder Posted July 12, 2005 Author Posted July 12, 2005 I tried what you said, but this didn't turn out to be the case for me. Since .Subtract return a TimeSpan type, then that's what your DateDiffVar needs to be declared as. But its .ToString() function doesn't accept parameters for formatting, nor does it have .Month, .Day, or .Year methods. Quote
Afraits Posted July 12, 2005 Posted July 12, 2005 Hmm, DateTime.subtract has 2 overloaded methods, when you pass a datetime var in it returns a timespan - while if you pass in a timespan it returns a datetime. So not as simple as I thought (mental note read the whole helpfile :D). Unless they've changed this for framework V2 I think you'll have to write your own Datediff or use the VB6 namespace. Need to think about this one a bit more. Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
JDYoder Posted July 12, 2005 Author Posted July 12, 2005 Unless they've changed this for framework V2 I think you'll have to write your own Datediff or use the VB6 namespace. *gasp!* Someone here actually told me to use the VB6 namespace!? ;) hehe. Just razzing ya. Seriously, thanks for all your help. Quote
JDYoder Posted July 13, 2005 Author Posted July 13, 2005 So now one knows of a .NET equivalent for DateDiff for values greater than a year? If not, no problem. I'll just use DateDiff. Hopefully framework V2 will expand the TimeSpan object to be more inclusive. Quote
jmcilhinney Posted July 14, 2005 Posted July 14, 2005 The reason that you can't use a TimeSpan to get anything greater than a number of days is because the TimeSpan knows nothing about when it started or when it finished. It only knows how long it is. Given that different months have different numbers of days and some years are leap years, the same period of time could give you different numbers depending on the start time and end time. If you wanted to implement your own DateDiff function, you would do it using the System.Globalization.Calendar class. My guess is that this is exactly how the Microsoft.VisualBasic.DateAndTime.DateDiff function is implemented. Quote
JDYoder Posted July 14, 2005 Author Posted July 14, 2005 If you wanted to implement your own DateDiff function' date=' you would do it using the System.Globalization.Calendar class. My guess is that this is exactly how the Microsoft.VisualBasic.DateAndTime.DateDiff function is implemented.[/quote'] Thanks for the input. In that case, I'll just use DateDiff, rather than reinvent the wheel that's already there. (And in this case, I'm sure my wheel wouldn't roll as smoothly!) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.