views:

352

answers:

3

Hi all,

I'm building a new ASP.NET web application based on a legacy one (Classic ASP). The people, who build this code 4, 5 years ago, used a lot of VBScript functions like DateDiff.

I'm currently working on a simulator that does a lot of calculations with dates. And I'm getting a annoying difference between these dates because I don't have a easily manner to get the difference between months in c# like they did in VB 6.

So, I choose the path of using the DateAndTime.DateDiff from Microsoft.VisualBasic namespace on my ASP.NET web application.

You guys know if there are some implications on that? I'm a little concerned with this approach.

Cheers,

+3  A: 

The only implication is that you're adding another assembly to your deployment, and using functionality that many C# developers (other people that might need to maintain your code) are not aware of. But personally I don't think there's much wrong with it. The MSDN Library documentation is typically good, and you can also add a few comments if you want to explain why your using it.

Edit: I also want to note that Microsoft.VisualBasic was implemented from scratch for .NET. It does not contain ancient code.

gWiz
We've outlawed the Microsoft.VisualBasic library in our shop just to force the holdovers to comply with the new .Net libraries.
Joel Etherton
The library contains efficient useful code. It's a library like any other, and is more "inspired by" Visual Basic than anything else. Why build, test, maintain, and version your own implementations when one already exists?
gWiz
I also agree with gWiz's comment from earlier this year; I think the Microsoft.VisualBasic namespace/assembly is a wonderful thing to help ease/speed the quick migration/porting of classic ASP/VBScript apps to .NET without having to rewrite things in a new manner every time you come across them...
Funka
A: 

It's good thing you are concerned with that, because you should not use any of of the classed in the VisualBasic namespace.

The .net library offer a much better solution called TimeSpan, use it in the following way: (dt1, and dt2 are DateTimes)

 TimeSpan ts = dt2 - dt1; //Or ts = dt1.Subtract(dt2)

As far as i would like to help, I really think a quick glance at the intellisence will tell you the rest (just write this in Visual Studio, and add ts.)

EDIT for real month counting: (y2 - y1) * 12 + m2 - m1, what the big deal?

Itay
Ok, but how does that handle months with 31 days, or February (both in a leap year and not in a leap year)?
Ken White
Yeah... that was the problem that I was facing... The results are a little bit diffent
Daniel
"you should not use any of of the classed in the VisualBasic namespace" - why not?
AakashM
you should not use VisualBasic name space because it's obsolete. and if you meant this kind of month counting, just: `(y2 - y1) * 12 + m2 - m1`
Itay
Can anyone confirm or provide references (links) as to whether the Microsoft.VisualBasic namespace is really considered obsolete? This answer http://stackoverflow.com/questions/226517/is-the-microsoft-visualbasic-namespace-true-net-code/226708#226708 seems to suggest No. (But is older than this already-one-year-old comment so may itself be out-of-date?.)
Funka
A: 

One possibility is to use the TimeSpan as Itay indicated, but divide by 30.4375 (the average days per month), ignoring fractional months. This is close enough for things I'm currently working on, but may not be accurate enough based on your needs. For instance, The difference between February 1 and March 1 would be 0, which (based on a 30.4375 day month definition) is correct; it may not be correct for your particular purposes, however.

Ken White