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 simulato开发者_开发知识库r 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,
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.
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.
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 DateTime
s)
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?
精彩评论