FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour datatime
Posts: 1396
Joined: Mon May 14, 2007 09:49 AM

datatime

Posted: Tue May 19, 2026 12:56 PM

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)

Posts: 44229
Joined: Thu Oct 06, 2005 05:47 PM

Re: datatime

Posted: Tue May 19, 2026 01:46 PM

Dear Yuri,

When you subtract two DATETIME values in Harbour, the result is a numeric value representing the difference in days. The integer part is the number of full days, and the fractional part represents the time difference as a portion of a 24-hour day 1 2 .

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 .


---

How the Difference is Calculated

The Harbour Virtual Machine handles the subtraction of two DATETIME types in hb_vmMinus 4 .

  1. Internal Calculation: It subtracts the internal Julian day count and the millisecond count separately 5 .
  2. Packing the Result: If there is a time difference, it "packs" these two values into a single double 6 .
  3. The Formula: The resulting number is calculated as days + (milliseconds / 86,400,000) 7 .

Making the Result Readable

Since the result is in "days", you can convert it using these constants defined in the codebase 8 :

UnitConversion Factor
Hoursdiff * 24
Minutesdiff * 1440
Secondsdiff * 86400

Example

If dDiff = datetime2 - datetime1:

  • Int(dDiff) gives the number of full days.
  • (dDiff % 1) * 24 gives the remaining hours.

Technical Constants

The system uses a default precision of 6 decimal places for these differences to ensure millisecond accuracy 9 .

Notes

  • If the time components of both variables are identical, the result will be a simple integer representing the day difference 10 .
  • This logic is also applied during compiler optimizations for constant expressions 11 .

Wiki pages you might want to explore:

Citations

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)

Code (text): Select all Collapse
#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;
      }
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 8559
Joined: Tue Dec 20, 2005 07:36 PM

Re: datatime

Posted: Tue May 19, 2026 04:42 PM

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 /END

Regards, slaudos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1396
Joined: Mon May 14, 2007 09:49 AM

Re: datatime

Posted: Wed May 20, 2026 07:31 AM

Thanks, I get it!

Continue the discussion