Click to See Complete Forum and Search --> : Calling VB brains, do it yourself date and time function


February 29th, 2000, 05:28 PM
Hello, I have a problem for the VB brains out there. I need to write a function that will accept 2 dates and times, and it needs to return the difference in days, and the amount of time passed in 100ths of seconds. If the dates and times are the same, the function should return zero. The variable dates and times will be passed in no specific order, so it will have to be determined which is the earlier date and time. I know about the existing date functions supplied by VB, I need to write the code without these functions. Anyone have a solution?? Or some good starting points??

February 29th, 2000, 07:11 PM
HELLO I AM HERE BECAUSE HE SAID CALLING ALL VB BRAINS AND WHEN I READ THAT I JUST LOOKED AROUND AND ONLY SAY MYSELF SITTING THERE SO THEREFORE I AM THE ONE AND BEST AND ONLY VB BRAIN THAT CAN HELP YOU. WELL ANYWAY ON TO YOUR PROBLEM (YOU GUYS HAVE SO MANY....HEHE CUTE LIL BEGINNERS). THE FUNCTION YOU NEED FOR 2 DATES IS IN THIS CODE I AM WRITING HERE.
FUNCTION WRITE()
INT MAIN()
FORM2.SHOW FUNCTION WRITE=TRUE
IF FORM2.SHOW FUNCTION WRITE=FALSE THEN
ORDER KILL.TRUE ON FUNCTION
ELSE
'THIS I WHERE IT GETS TRICKY
UNDO KILL.TRUE
THERE MY FRIEND I THINK THAT SHOULD WET YOUR WHISTLE ENOUGH FOR TODAY....WELL TELL ME HOW IT GOES! GOOD LUCK!

AndyK
February 29th, 2000, 07:50 PM
*** was that? that was BS not a VB code, looks like some parts form C++ but it's basically all garbage, so Mr.VbBrain, <beep> wrong answer!!!

February 29th, 2000, 08:02 PM
HEY MAN THIS IS A FRIENDLY WEBSITE SO JUST BECAUSE YOU CANNOT WRITE MASTER CODE SUCH AS I DO THAT DOESN'T MEAN YOU HAVE TO CRY OK?

AndyK
February 29th, 2000, 08:07 PM
You got problems with atitude, you got to go see a doctor or something......

February 29th, 2000, 09:09 PM
LOL well that was amusing, but I really was hoping for some good ideas. Do you have any thing to share Andy?

February 29th, 2000, 09:12 PM
Hi Andy, if you have any good ideas about this, I'd love to hear them. The other code submitted was obviously a joke.

Chris Eastwood
March 1st, 2000, 02:47 AM
This is quite straightforward using the Abs function and the DateDiff function that VB provides, eg:


'
' Assumes that dteDate1 and dteDate2 already have a value and are
' of the VB type 'date'
'
MsgBox "Days Difference between Dates = " & Abs(DateDiff("d", dteDate1, dteDate2))
MsgBox "Hours Difference between Dates = " & Abs(DateDiff("h", dteDate1, dteDate2))
MsgBox "Minutes between dates = " & Abs(DateDiff("n", dteDate1, dteDate2))
MsgBox "Seconds between dates = " & Abs(DateDiff("s", dteDate1, dteDate2))
'




- using the method above, it doesn't matter which date is the most recent or in which order.


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

March 1st, 2000, 06:41 AM
Thank you Chris, I was hoping to be able to write the function without the use of predefined VB functions, but your input is appreciated.

Kyle Burns
March 1st, 2000, 07:00 AM
You really don't want to not use the intrinsic VB date functions. I've had to maintain code where the programmer didn't know of the built in date/time functions and it was a nightmare. There were three integer variables (dd, mm, and yy) and he went through all sorts of hoops to get where he wanted to go. DateDiff took him nearly 40 lines of code and the logic was flawed (did not allow for leap years). Bottom line is, you cannot write most functions that VB offers more efficiently than they already are. That said, how about something like this:

public Function Foo(dtDate1 as date, _
dtDate2 as date, _
byref iNumDays as Integer, _
byref iNumSeconds as Integer) as Integer
'******************************
' date Data Type Only Stores
' to the second, so we'll only
' compute to the second instead
' of the 1/100th
'*******************************
iNumDays = Abs(DateDiff("d", dtDate1, dtDate2))
iNumSeconds = Abs(DateDiff("s", dtDate1, dtDate2))
'The return value will be zero if the
'dates are on the same day
Foo = iNumDays
End Function

Dark Sean
March 1st, 2000, 07:27 AM
Thanks Kyle Ill try to give it a shot, much appreciated!