Time addition with numbers

Chipcom

Registered User.
Local time
Today, 19:53
Joined
Apr 13, 2006
Messages
63
Hi

I need to know how to do simple addition:
[timevar]=[timevar]+[number]

example:


8:00+9=17:00

Thanks
 
TIMESERIAL(hours, mins, secs) converts numbers to time.

So..
[timevar]=[timevar]+timeserial([number],0,0)

hth
Stopher
 
minutes addition

Thanks

But I have another problem:
I Can't add minutes to a hour for example:

[timeVar]= [timeVar]+[timeVar]

08:00:00+10 minutes

I converted 10 to 00:10:00 but when I add it to a hour I get Mismatch.

I need that :

08:00:00+10 minutes would be equal to 08:10:00

What I did:

[timeVar]= [timeVar]+TimeSerial(0,[timeVar],0)

But I get Mismatch

Can you please help me with that

Thanks
 
I can't see any problem with your logic. Perhaps you could post a small version of your d/b.

Stopher
 
How about using DateAdd() which is designed to do what you want?
 
Times Subtraction

Thanks you

But I need now to subtract times:

[TimeVar]=[Time1]-[Time2]

10:03:00=18:03:00-8:00:00

Thanks
 
Hi

Sorry It doesnt work, I get type mismatch.

What I did:


DateAdd("h",-Time1,Time2)


Can you please help me with this problem?

Again I need (for example)

10:03:00=18:03:00-08:00:00
 
First, there is a DateSubtract.

Second, there is this to consider. You can convert the items yourself if you understand date/time variables.

A date/time variable is what is called a CAST (a.k.a. TYPECAST) of a DOUBLE. The date/time in the variable is the days (integer part) and fractions of a day (fractional part) since a reference date. The trick is that the date formatting routines have to figure out the number of years, months, days, hours, minutes, and seconds represented by the DOUBLE.

The good news is that if you convert the times in hours, minutes, and seconds to fractions of a day, they add directly. The bad news is that if they represent time intervals but not necessarily dates, the formatting routines really don't like them.

Constants you need to remember:

If adding hours, convert to DOUBLE and divide by 24.

If adding minutes, convert to DOUBLE and divide by 1440.

If adding seconds, convert to DOUBLE and divide by 86400.

Once you have the fractions, you can do what you wish with them. As long as there is no integer part to the fraction, the Format$ routine will allow you to specify a user-defined time format. If there is an integer part, you have exceeded one day's worth of hours.
 
I need an example of the command that return the result of the subtraction

Hi

Maybe I didn't understand you but there is no DateSubtract command.
I understand that DateAdd command can subtract between two dates/times and when I tried to use this commad I got type mismatch becouse of the minus sign (the is no negative time).
I understand that dateadd command can subtract from time if you subract number (not a date/time) from the time.


Maybe there is no command that can do the subtraction of two times and therefore I may program a new function that can do that calculation.

Thanks
 
There's a DateDiff() function. I'm not aware of a DateSubtract() function but it may be there in later versions of Access than I have (A2003).
 
This is not the solution

I need that the function will rerurn time value format (like 10:05:35) and not just number (like 10)

Thanks
 
Time Addition

Hi

I need to add between two times, like this:

timeVar=[TimeVar1]+[TimeVar2]

17:05:50=08:01:10+09:04:40

Any idea how?


Thanks
 
Hi

I need to add between two times, like this:

timeVar=[TimeVar1]+[TimeVar2]

17:05:50=08:01:10+09:04:40

Any idea how?


Thanks

This SHOULD do it for you. I haven't tested it thoroughly so I can't say that there aren't situations that it might not work properly on. Give it a go and see if it works for you.

Code:
Public Function AddTimes(dteTime1 As Date, dteTime2 As Date) As Date
    Dim inthr1 As Integer
    Dim intmin1 As Integer
    Dim intsec1 As Integer
    Dim inthr2 As Integer
    Dim intmin2 As Integer
    Dim intsec2 As Integer
    Dim inthrNew As Integer
    Dim intminNew As Integer
    Dim intsecNew As Integer
    
    inthr1 = Format(dteTime1, "h")
    intmin1 = Format(dteTime1, "n")
    intsec1 = Format(dteTime1, "s")
    
    inthr2 = Format(dteTime2, "h")
    intmin2 = Format(dteTime2, "n")
    intsec2 = Format(dteTime2, "s")
    
    inthrNew = inthr1 + inthr2
    intminNew = intmin1 + intmin2
    intsecNew = intsec1 + intsec2
    
    AddTimes = TimeSerial(inthrNew, intminNew, intsecNew)
Exit Function

EDIT:
Actually, I did a fair amount of playing with this and it appears to do a great job of doing the trick.
 
Last edited:
DateDiff is the correct name. Sometimes I get a senior moment.
 
Code:
I need to add between two times, like this:

timeVar=[TimeVar1]+[TimeVar2]

17:05:50=08:01:10+09:04:40

Any idea how?

You can enclosed the time literals within # signs.

TimeVar = #08:01:10# + #09:04:40#


Note:
Access may change the time literals to the regional time format of your system.

Access will also display TimeVar in regional time format. If you use the Format() function to format it to "hh:nn:ss", what you get will be text string.
.
 
Last edited:
How to Add String Of Times?

Hi

Thanks For your answers.

I need know to add strings, how to add for example:

245:27+153:12 ?
(The left side is hours and the right is minutes)


Thanks
 
Hi

I need to know how to do simple addition:
[timevar]=[timevar]+[number]

example:


8:00+9=17:00

Thanks

Hi,

Your [Number] field needs to be of the same data type as your [timevar] field, i.e. make your [number] field of double type:

8:00 + 9:00 = 17:00


FAB

John
 
I need know to add strings, how to add for example:

You can't.

OK, now that your bubble has burst, you have to consider a smart conversion routine. It must convert its input strings into a convenient number format such as, say, DOUBLE. But for smaller time amounts, LONG also works and might be easier to use because of the mathematical imprecision of representing REAL numbers in fixed-width holders of a different radix internally than the external number. I.e. representation errors in floating-point.

OK, so onceconverted, you add the numbers post-conversion, then write one more conversion routine to convert the numbers back to strings.

If you attempt to add strings, you often get trash but sometimes you get a concatenation effect. You almost NEVER get what you really wanted.
 
I find that if I try to add strings, all I get is a tangled mess (sorry for that one, I just couldn't resist
mad-grin.gif
 

Users who are viewing this thread

Back
Top Bottom