DateDiff VB6 function equivalent Vb.net

Posted on 2012-03-25
Medium Priority
Last Modified: 2012-03-25
Hi ,

I have try the create and exact equivalent of old DateDiff in VB.net but not work:


    Const DATE_YEAR = "yyyy"  
    Const DATE_QUARTER = "q"      
    Const DATE_MONTH = "m"      
    Const DATE_DAY_OF_YEAR = "y"  
    Const DATE_DAY = "d"          
    Const DATE_WEEKDAY = "w"    
    Const DATE_WEEK = "ww"      
    Const DATE_HOUR = "h"      
    Const DATE_MINUTES = "n"    
    Const DATE_SECONDS = "s"      

    Public Function S_DateDiff(Interval As String, Date1 As Date, Date2 As Date) As Integer

        Dim TmpDateStart As DateTime = Date1
        Dim TmpDateEnd As DateTime = Date2

        Dim DateDifference As TimeSpan = TmpDateEnd.Subtract(TmpDateStart)

        Dim LInterval As String

        LInterval = Lcase(Interval)

        Select Case LInterval

            Case DATE_DAY, "4"
                Return DateDifference.Days

            Case DATE_WEEK, "6"
                Return DateDifference.Days * 7

            Case DATE_HOUR, "7"

                MsgBox(DateDiff(Interval, Date1, Date2))

                Return DateDifference.Hours

            Case DATE_MINUTES, "8"
                Return DateDifference.Minutes

            Case DATE_SECONDS, "9"
                Return DateDifference.Seconds

            Case Else
                Return ""

        End Select


When I pass the "h" interval (hour) this function return a different value of the original VB6 function.

Someone can help me to fix this problem ?

Thank you
Question by:luca345
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 37762766
You want TotalHours(), not Hours().  *Same thing with Mintues and Seconds.

Author Closing Comment

ID: 37762805
Thank you !

I need in Vb.net an exact clone (new function) of the old Vb6 "format" function.

If I post this thread are you interested ?
LVL 40
ID: 37763567
You have posted a few of these requests for VB6 clones, and I wonder why you are spending time on that. Is it a school exercise? Then it is a good one.

Otherwise, why are you spending time on such a useless endeavour?

I understand that you are not allowed to use the old VB6 functions in your code. And personnally, I feel that this is the right way to do things. It eases the transition and exchange of information between languages and goes directly to the framework instead of going through an intermediary, improving performance.

And except for a few exceptions, the .NET functions that replace the old one are always easier to use and understand, and are better most of the time.

So why no simply use the ones provided by .NET?

If it is a question of copying old VB6 codes to .NET because you find that the converter does not does a good job, then you are losing your time. The converter does as good a job as it can. The problem of converting a VB6 appliction to .NET is not a problem of syntax, it is a problem of design. Applications in .NET are not designed the same way as they were in VB6 because the framework does not answer to the same rules.

Any type of direct conversion will end up being a maintenance nightmare with very bad performance. Trying to do better than the people who made the converter won't solve any problem, it risks being worse because you might not understand the challenge as well as they did.

Most of the time, rewriting an application in .NET instead of trying to convert it ends up taking less time while giving you applications with a better performance and easier maintenance. And this, now matter what you use to make the conversion.

Author Comment

ID: 37763613
Hi JamesBurger ,

4 month ago I was start to convert a VB6 project to VB.net.

1 month ago I was finish, but the new .NET project contain 30/40% of VB6 functions.

Now the some project contain only 2 VB6 old instructions:

1) Api 'GetWindowThreadProcessId'
2) 'Format'

I need to find a clone of 'Format' function and a way to replace the 'GetWindowThreadProcessId' Api.

When I will do it the project is 100% pure VB.net , no api no win32 calls.

And after I can convert it to C# and then I can import this to monoproject and the application can run to all platform (win , mac , linux)

In all of this step I learn many thing.

This is my plan.

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question