Consequences of using VB.NET DateAndTime.DateDiff method from C #

I am creating a new ASP.NET web application based on the old one (Classic ASP). The people who build this code 4.5 years ago used a lot of VBScript functions like DateDiff.

I am currently working on a simulator that does a lot of computation with dates. And I get annoying difference between those dates because I don't have an easy way to get the difference between months in C # like in VB 6.

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

Do you guys know if there are any implications for this? I am a bit concerned about this approach.

Greetings,

+2


source to share


3 answers


The only implications are that you are adding another assembly to your deployment and are using functionality that many C # developers (other people who might need to maintain your code) do not know. But personally, I don't think there is anything wrong with him. The MSDN library documentation is usually good, and you can also add some comments if you want to explain why you are using it.



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

+3


source


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



0


source


The good thing is that you are worried about this, because you shouldn't be using any of the classes in the VisualBasic namespace.

The .net library offers a much better solution called TimeSpan, using it like this: (dt1 and dt2 - DateTime

s)

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

      

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

)

EDIT to calculate real month: (y2 - y1) * 12 + m2 - m1

what's the big deal?

-1


source







All Articles