Solved

DateDiff VB6 function equivalent Vb.net

Posted on 2012-03-25
4
886 Views
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
0
Comment
Question by:luca345
  • 2
4 Comments
 
LVL 85

Accepted Solution

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

Author Closing Comment

by:luca345
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 ?
0
 
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.
0
 

Author Comment

by:luca345
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

679 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