473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Can someone fix this time subtractor to return negative value

228 100+
this is the code which I found online and works great. I forgot its author but whoever you are, 10x but a lil help would be appreciated...

here is the code

Expand|Select|Wrap|Line Numbers
  1. 'public variables
  2. Const MSecInSec As Double = 1000
  3. Const SecsInMin As Double = 60
  4. Const MinsInHour As Double = 60
  5. Const HoursInDay As Double = 24
  6. Const SecsInDay As Double = HoursInDay * MinsInHour * SecsInMin
  7. Const mSecMult As Double = 1 / (SecsInDay * MSecInSec)
  8.  
  9. private sub command1_click()
  10.  
  11. msgbox  ReturnTimeDifference("01:00:00:00","04:30:00:00")
  12. msgbox  ReturnTimeDifference("04:00:00:00","01:30:00:00")
  13.  
  14.  
  15. end sub
  16.  
  17.  
  18. Public Function ReturnTimeDifference(ByVal smalltime As String, ByVal bigtime As String) As String
  19. Dim TimeA As Date
  20. Dim TimeB As Date
  21.  Dim Res As Double
  22.  Dim timeparts As Variant
  23.  Dim timepartsb As Variant
  24.  Dim ms As String
  25.  Dim ms2 As String
  26.  timeparts = Split(smalltime, ":")
  27.  timepartsb = Split(bigtime, ":")
  28.  ms = timeparts(3)
  29.  ms2 = timepartsb(3)
  30.  
  31. ms = ms & String(3 - Len(ms), "0")
  32. ms2 = ms2 & String(3 - Len(ms2), "0")
  33.  
  34.  
  35.  
  36.  
  37.  TimeA = TimeSerialEx(timeparts(0), timeparts(1), timeparts(2), ms) '01:04:12.2
  38.  TimeB = TimeSerialEx(timepartsb(0), timepartsb(1), timepartsb(2), ms2) '05:08:15.55
  39.  
  40.  Res = Format$((TimeB - TimeA) * SecsInDay, "0.00") 'RETURNS POSITIVE NO MATTER WHAT!
  41.  
  42. ReturnTimeDifference = Format$(Res / SecsInDay, "hh:nn:ss:") & _
  43.  Round(Res - Fix(Res), 2) * 100 'avoids the decimal point
  44.  
  45. End Function
  46.  
both return positive value. he had commented this line as "returns postive no matter what

Expand|Select|Wrap|Line Numbers
  1.  Res = Format$((TimeB - TimeA) * SecsInDay, "0.00") 'RETURNS POSITIVE NO MATTER WHAT!
  2.  
but i can't seem to figure out which part of it makes Res positive always.

any tips?
Mar 12 '11 #1
5 2002
Rabbit
12,516 Expert Mod 8TB
The format 0:00 makes it return only positive numbers since it doesn't allow for the negative sign.
Mar 13 '11 #2
samvb
228 100+
i thot about it and removed it and tweaked it but nothing changed. can u help me out a lil bit?
Mar 13 '11 #3
Rabbit
12,516 Expert Mod 8TB
I don't know the formats it allows. You may have to look that up but you can try 0:00;-0:00.
Mar 13 '11 #4
MikeTheBike
639 Expert 512MB
Hi

As pointed out by Rabbit the Time format does not take account of the sign and only returns +ve values.
However, contrary to the statement in the code this line
Expand|Select|Wrap|Line Numbers
  1. Res = Format$((TimeB - TimeA) * SecsInDay, "0.00")
does return -ve values, so the only suggestion I have is adding this line to the bottom of the function
Expand|Select|Wrap|Line Numbers
  1. If Res < 1 Then ReturnTimeDifference = "-" & ReturnTimeDifference
  2.  
??

MTB
Mar 15 '11 #5
whodgson
542 512MB
As JosAH has pointed out somewhere you may wish to research JD (for Julian day) where you can convert a date to JD then add (or subtract) the number of days and then convert the new JD back to date. This is simply done.
Mar 24 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
0
by: Koji Ishii | last post by:
According to the doc, Stream.Read() returns: - # of bytes read, or - 0 for EOF But could that be a negative value? If so, how should we program against such case? I have a loop like this:...
2
by: Angus Comber | last post by:
Hello Sorry this really is a newbie question! I am doing some floating point arithmetic and calculating the time difference between two dates. The date values being comparied are actually...
4
by: OutdoorGuy | last post by:
Greetings, I am attempting to compile the code below, but I am receiving an error message when I do so. The error message is: "CSO161: 'Forloop.CalcAvg(int)': Not all code paths return a...
0
by: Bill Littman | last post by:
We are using a DB2 7.2 system. On the DB2 Control Center of a server, we are viewing the details of the tablespaces. The "percentage used" column is showing a negative value(-69) for one of the...
4
by: Peter | last post by:
Hi I am using DateTime class and TimeOfDay.Hours attribute to return current time. It returns right value on two servers, but in one particular server it does not return right time. I checked that...
35
by: Martin Wells | last post by:
Plain char may be signed or unsigned. Typical ranges could be: CHAR_MIN == -128, CHAR_MAX == 127 CHAR_MIN == 0, CHAR_MAX == 255 The Standard says that the behaviour is undefined if we...
8
by: mdeh | last post by:
Hi Everyone, Tried to post via Google..which once again seems to be fritzed...so please excuse if 2 posts show up. I am trying to understand why I am not getting a negative value back, using my...
1
by: vikastcser | last post by:
Hi All , I have a table where four rows are there and each row has two tds.one td is for label and two image icons and other td is used for keeping numeric values. I am able to accomodate digits...
4
by: Pachalo | last post by:
Morning... Am working on a simple billing system and a looking for a code that can help calculate the difference in time between sessions..(am using VB.Net ) This is what is happening: l...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.