Solved

DateDiff VB6 function equivalent Vb.net

Posted on 2012-03-25
4
894 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…

739 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