Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*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.

Students Click Here


Calculate time difference

Calculate time difference

Calculate time difference

How calculate time between 2 given times in Hour minute seconds


a = time()

b = time()

? b - a

RE: Calculate time difference

Don't use TIME(). Use DATETIME().

a = DATETIME() gives the current date and time, to the nearest second, as a variable of Datetime type.

b = DATETIME(2018, 6, 28, 8, 15) gives a specific time and date, of Datetime type.

Subtracting one datetime from another gives the difference in seconds:

Secs = b - a

To convert to hours minutes and seconds, divide by 3600 (this gives you the number of hours), then divide the remainder by 60 (gives minutes), and the final remainder is the number of seconds.


Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Calculate time difference

Just expand on that:

If diff is the difference (in seconds) between two datetimes, then:

CODE -->

hours = INT(diff / 3600)

minutes = INT((diff - (hours * 3600)) / 60)

seconds = diff - (hours * 3600) - (mins * 60) 

By the way, it is advisable not to use single letters between a and j as variable names. The reason is that they might clash with the built-in single-letter table aliases. In practice, it will very rarely cause a problem, but most developers try to avoid it.


Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Calculate time difference

And besides computing the elements of the time difference, an easy way to get from Seconds to HH:MM:SS is, of course, adding these seconds to a default date, eg:


dt1 = DATETIME(2018, 6, 28, 8, 15)
dt2 =  DATETIME(2018, 6, 28, 9, 35)
diffseconds = dt2-dt1
difft = DateTime(200,1,1,0,0,0)+diffseconds

? "The difference between", dt1, " and", dt2, " is", TToC(difft,2) 

Because you can not only get the diff in seconds, you can also add seconds to a DateTime to get another one. And if you do so for a DateTime at midnight, the time portion is exactly what the seconds turn to in hours, minutes and seconds, especially in SET HOURS TO 24 mod without AM/PM display this works for essential differences below a day. Enough for example for appointment durations, working time logging etc.

In the end, store DateTimes of the points in time involved for your case, and you'll always be able to compute differences and display them in a human-readable format.

Bye, Olaf.

Olaf Doschke Software Engineering

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close