Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO CALCULATE TIME DIFFERENCE 3

Status
Not open for further replies.

icecr3amc4k3

Programmer
Apr 25, 2023
7
ID
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.
Screenshot_56_cnchfy.png
 
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
 
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)

Visual FoxPro articles, tips and downloads
 
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 stating it's not required to use them. But:

help said:
...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
 
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

 
On the topic of time related functions, there is a nice collection here:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top