There are 2 variables of the datatime() type. How can I get the difference between them (if I subtract one from the other, I get some kind of incomprehensible number)
There are 2 variables of the datatime() type. How can I get the difference between them (if I subtract one from the other, I get some kind of incomprehensible number)
Dear Yuri,
When you subtract two
To convert this "incomprehensible number" into a human-readable format, you can multiply the result to get hours, minutes, or seconds, or use standard functions to decode the timestamp difference 3 .
The Harbour Virtual Machine handles the subtraction of two
Since the result is in "days", you can convert it using these constants defined in the codebase 8 :
| Unit | Conversion Factor |
|---|---|
| Hours | |
| Minutes | |
| Seconds |
If
The system uses a default precision of 6 decimal places for these differences to ensure millisecond accuracy 9 .
Wiki pages you might want to explore:
File: src/vm/hvm.c (L3401-3401)
static void hb_vmMinus( PHB_ITEM pResult, PHB_ITEM pItem1, PHB_ITEM pItem2 )File: src/vm/hvm.c (L3435-3442)
else if( HB_IS_DATETIME( pItem1 ) && HB_IS_DATETIME( pItem2 ) )
{
long lTime = pItem1->item.asDateTime.time -
pItem2->item.asDateTime.time,
lJulian = pItem1->item.asDateTime.julian -
pItem2->item.asDateTime.julian;
if( lTime != 0 )
hb_itemPutNDDec( pResult, hb_timeStampPackDT( lJulian, lTime ), HB_TIMEDIFF_DEC );File: src/vm/hvm.c (L3447-3447)
HB_ITEM_PUT_LONGRAW( pResult, lJulian );File: src/common/hbdate.c (L930-935)
double hb_timeStampPackDT( long lJulian, long lMilliSec )
{
HB_TRACE( HB_TR_DEBUG, ( "hb_timeStampPackDT(%ld, %ld)", lJulian, lMilliSec ) );
return ( double ) lJulian +
( double ) lMilliSec / HB_MILLISECS_PER_DAY;File: include/hbdate.h (L157-161)
#define HB_MINUTES_PER_DAY ( 24 * 60 )
#define HB_SECONDS_PER_DAY ( HB_MINUTES_PER_DAY * 60 )
#define HB_MILLISECS_PER_DAY ( HB_SECONDS_PER_DAY * 1000 )
#define HB_TIMEDIFF_DEC 6 /* default number of decimal places in numeric timestamp diff values */File: src/common/expropt2.c (L521-535)
{
long lTime = pLeft->value.asDate.lTime - pRight->value.asDate.lTime,
lDate = pLeft->value.asDate.lDate - pRight->value.asDate.lDate;
if( lTime == 0 )
{
pSelf->value.asNum.val.l = lDate;
pSelf->value.asNum.bDec = 0;
pSelf->value.asNum.NumType = HB_ET_LONG;
}
else
{
pSelf->value.asNum.val.d = hb_timeStampPackDT( lDate, lTime );
pSelf->value.asNum.bDec = HB_TIMEDIFF_DEC;
pSelf->value.asNum.NumType = HB_ET_DOUBLE;
}https://fivetechsupport.com/forums/viewtopic.php?t=45778
// C:\FWH2603\SAMPLES\DIFDATTI.PRG
#include "FiveWin.ch"
FUNCTION Main()
LOCAL tInicio := hb_DateTime( 2026, 5, 19, 08, 00, 00 ) // 19/05/2026 08:00:00
LOCAL tFim := hb_DateTime( 2026, 5, 19, 10, 30, 45 ) // 19/05/2026 10:30:45
LOCAL nDiffDias, nTotalSegundos, nHoras, nMinutos, nSegundos
// 1. Subtração principal (retorna a diferença em dias com fração)
nDiffDias := tFim - tInicio
// 2. Converter o total para segundos (1 dia = 86400 segundos)
nTotalSegundos := nDiffDias * 86400
// 3. Separar em Horas, Minutos e Segundos
nHoras := Int( nTotalSegundos / 3600 )
nMinutos := Int( ( nTotalSegundos - ( nHoras * 3600 ) ) / 60 )
nSegundos := Int( nTotalSegundos - ( nHoras * 3600 ) - ( nMinutos * 60 ) )
// Exibir resultado
MsgInfo( "Diferença: " + Str(nHoras, 2) + ":" + Str(nMinutos, 2) + ":" + Str(nSegundos, 2), "Tempo Decorrido" )
RETURN NIL// FIN /END
/*
LOCAL tStart := {^ 2026-05-19 12:00:00}
LOCAL tEnd := {^ 2026-05-19 14:30:00}
LOCAL nDiff := tEnd - tStart // Returns the difference as days with fraction
// 1. Get exact difference in SECONDS
nSeconds := nDiff * 86400
// 2. Get exact difference in MINUTES
nMinutes := nSeconds / 60
// 3. Get exact difference in HOURS
nHours := nSeconds / 3600
*/#include "FiveWin.ch"
FUNCTION Main()
LOCAL tStart := HB_DateTime(2026, 05, 19, 08, 00, 00)
LOCAL tEnd := HB_DateTime(2026, 05, 19, 10, 30, 45)
LOCAL nDiff := tEnd - tStart
LOCAL nTotalSec := nDiff * 86400
LOCAL nHours := INT(nTotalSec / 3600)
LOCAL nMinutes := INT((nTotalSec % 3600) / 60)
LOCAL nSeconds := INT(nTotalSec % 60)
MsgInfo("Start Time: " + TTOC(tStart) + CRLF + ;
"End Time: " + TTOC(tEnd) + CRLF + CRLF + ;
"Difference: " + LTRIM(STR(nHours)) + "h " + ;
LTRIM(STR(nMinutes)) + "m " + ;
LTRIM(STR(nSeconds)) + "s", ;
"Time Difference")
RETURN NIL
// FIN /ENDRegards, slaudos.
Thanks, I get it!