×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# HOW TO CALCULATE TIME DIFFERENCE3

## HOW TO CALCULATE TIME DIFFERENCE

(OP)
Hi everyone ,i am a student who learn foxpro so i have a question..
i have an assigment to make parking ticket program and i dont know how to calculate time difference
example :
car parking at 7.00(text1) and car out at 9.00(text2)
so i want to see the result at text3(result) this is the code i tried but failed

(form) (active)
thisform.text1.Value = DATETIME ()
thisform.text2.Value = DATETIME ()

(comand1) (click)
a = INT(thisform.text1.Value)
b = INT(thisform.text1.Value)
result = b - a
thisform.text1.Value = result

in this case i just want to calculate the minute,where the results every 60 minute are multiplied by (IDR 4000)
thank for everyone to sees and answer my question i hope god bless you all.

### RE: HOW TO CALCULATE TIME DIFFERENCE

Hello merpati89,

you can't simply convert a datetime data type to int, i.e. INT(DATETIME()) errors.

But you can simply subtract one of another datetime in VFP, the differnce is the number of seconds between the two datetimes. So to get minutes you can divide by 60.

Chriss

### RE: HOW TO CALCULATE TIME DIFFERENCE

#### CODE -->

thisform.text1.Value = DATETIME()
thisform.text2.Value = DATETIME() 

That code will simply place the current date and time in the two texboxes. And then, when you subtract one from the other, you will get zero.

What you need to do is to let the user enter each of the datetimes in turn. So you can use the above code to get the initial values and establish the data type. The user will then overwrite them with the actual start and end times. Then, in your command button, subtract one from the other, to give the time difference in seconds, and divide the result by 60 to get it in minutes:

#### CODE -->

* Click event of command button
LOCAL lnResult
lnResult = (thisform.text2.Value) - thisform.text1.Value) / 60

* Display the result, for example in a label
thisform.label1.caption = lnResult 

You will need to add a label to the form to receive the result. Or it could be a textbox, but a label is more useful because it will not need to be edited.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

### RE: HOW TO CALCULATE TIME DIFFERENCE

There's another trick you can use, if you want to display the difference betweeen two datetimes in the form of a time hh:mm:ss, you can simply take any datetime at midnight and add the seconds to it. As long as the time is below 24 hours, the time portion then is simply what you'd display.

So in short and as summary of both things:

#### CODE -->

Local lnMinutes && number of minutes
Local lcTimeDisplay && string in format hh:mm:ss up to 23:59:59
lnMinutes = (thisform.text2.Value - thisform.text1.Value)/60
lcTimedisplay = Right(Ttoc(DtoT(Date())+(thisform.text2.Value - thisform.text1.Value),3),8) 

By the way, it makes sense to rename controls from the automatic names they get. Every code becomes more readable and understandable if you do so. For example Text1 could be called txtDateTime1 or txtStartTime - you better know what this textbox is displaying. Likewise Text2. There is a chapter in the VFP help about naming conventions https://www.vfphelp.com/help/_5wn12pc2u.htm, stating it's not required to use them. But:

#### Quote (help)

...as part of good programming habits and to help remind you of the type of data you are working with

It really would also help me/us here to know what is the intention of Text1 and Text2. I think you're just tinkering with VFP to learn about it. Then that's a good entry point about the topic of writing code for sharing. If everyone uses these conventions it's simpler to understand code and talk about it.

I can also say that it is not all that helpful to do everything as recommended by this chapter. For example, you need a bit to get used to the variable name convention with two prefix letters. And I also don't use them in all code. If you dig into it, the convention VFP proposes also won't specify the data types of the values of textboxes, which contradicts this quote. The data type is only a prefix of variables and fields of tables, not of controls. So that also points out this proposed convention is not perfectly thought to the end.

On the other side, you should know that single-letter names can really lead to problems. A and B (for backward compatibility) are also always the names of workarea number 1 and 2. So indeed your usage of A and B has more danger than sticking to Text1 and Text2 control names. It'll not lead to problems immediately, you're allowed to use single letter variable names and using them works, you won't get the idea that a.fieldname then can address a table in workarea 1, because you don't set a to an object, but you see how that can lead to problems when a.x could mean property x of object a or field x of the dbf open in workarea 1.

The overall simplest advice I recommend is to be verbose about the names you give things, with exceptions where long names are just not making things easier to read. For example, it is still okay to name a counter just lnI for local numerical variable I. The single-letter names of the first few workareas ends at j, so actually i or I is still ambiguous. Say you would like to see which alias names are used by workareas, then doing a loop like this would fail:

#### CODE

For i = 1 to 20
Select i
? Alias()
EndFor 
It won't error, but it would display the alias name of workarea i 20 times.
On the other hand, this will work:

#### CODE

For i = 1 to 20
? Alias(i)
EndFor 
Here i is taken as the counter value.

So you're not bound to fail with this, nor get errors, but unexpected behaviour hard to fuigure out. So get into good habits from the getgo and avoid even to use letters k-z.
There are enough gotachas in the way VFP works, you can avoid contributing to them by getting used to better naming.

Chriss

### RE: HOW TO CALCULATE TIME DIFFERENCE

Hi merpati89,

There are two TIMER controls on you form. What are they good for? Do they trigger the textboxes' values?

The calculation of the difference would be according to Mike's and Chriss's suggestions

hth

MarK

### RE: HOW TO CALCULATE TIME DIFFERENCE

On the topic of time related functions, there is a nice collection here: https://github.com/RickBorup/FoxDates/blob/master/...

What you won't find in this collection of functions is to calculate differences in seconds, minutes, hours, days, etc. as it's such a simple thing in VFP to simply subtract a datetime from another. By the way subtracting two dates from one another you get the difference in days, which I think is better than a large number of seconds.

But notice there's a strict rule built into it, you can only get differences of two dates or two datetimes, not of mixed types, though a date could be interpreted as a datetime with midnight as its time portion. You also won't get a datetime from addding a non integer to a date, so DATE()+1.5 is not a datetime in a day with 12PM as a time portion.

Chriss

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!