C++ ctime difftime() Function
Example
Measure the difference in time between two timestamps:
struct tm date;
time_t now;
time_t before;
// Create a timestamp for right now
time(&now);
// Create a timestamp for 5 hours ago
date = *localtime(&now);
date.tm_hour -= 5;
before = mktime(&date);
// Calculate the difference between the two timestamps in seconds
cout << difftime(now, before);
Try it Yourself »
Definition and Usage
The difftime()
function returns the number of seconds between two timestamps.
If the start timestamp is greater than the end timestamp then the result is a negative number.
The difftime()
function is defined in the <ctime>
header file.
Syntax
difftime(time_t end, time_t start);
The time_t
data type represents a number.
Parameter Values
Parameter | Description |
---|---|
end | Required. The timestamp of the end of the time interval being measured. |
start | Required. The timestamp of the start of the time interval being measured. |
Technical Details
Returns: | A double value representing the number of seconds between the two timestamps. |
---|