Number of days between two dates

82 views
Skip to first unread message

kofa kofa

unread,
Jan 2, 2026, 11:23:24 AMJan 2
to The Ring Programming Language
Hello everyone
Happy New Year
I'm looking for a code that calculates the difference between an old date and today's date. For example, I want to calculate the number of days from April 20, 1922, to today's date.

Bert Mariani

unread,
Jan 2, 2026, 11:28:52 AMJan 2
to The Ring Programming Language
Hello Kofa

See Ring Documentation:   Section 19.7

19.7 DiffDays() Function

Syntax:

DiffDays(cDate1,cDate2) ---> number of days (Date1 - Date2)

Example:

cDate1 = date()

see cDate1 + nl # 24/05/2015

cDate2 = adddays(cDate1,10)

see cDate2 + nl # 03/06/2015

see "DiffDays = " + diffdays(cDate1,cDate2) + nl # -10

see "DiffDays = " + diffdays(cDate2,cDate1) + nl # 10

kofa kofa

unread,
Jan 2, 2026, 11:44:50 AMJan 2
to The Ring Programming Language
I tried many times, but it didn't work with old dates.

kofa kofa

unread,
Jan 2, 2026, 11:46:34 AMJan 2
to The Ring Programming Language
Can you write the code that calculates the number of days from 20/4/1922?

Mahmoud Fayed

unread,
Jan 2, 2026, 12:01:09 PMJan 2
to The Ring Programming Language
Hello Kofa

See this example:

diffdays.png

Greetings,
Mahmoud

Bert Mariani

unread,
Jan 2, 2026, 12:16:58 PMJan 2
to The Ring Programming Language
Hello Mahmoud,  Kofa

Looks like there is a Bug in DiffDays()
Has to do with the  Unix Epoch date January 1, 1970

OldDate..: 20/04/1922
Today....: 02/01/2026
DaysSince: 0

OldDate..: 20/04/1992
Today....: 02/01/2026
DaysSince: 12310

OldDate..: 20/04/1972
Today....: 02/01/2026
DaysSince: 19615

OldDate..: 20/04/1965
Today....: 02/01/2026
DaysSince: 0

DaysSince-1.ring

Bert Mariani

unread,
Jan 2, 2026, 12:25:41 PMJan 2
to The Ring Programming Language

DiffDays()  Bug

OldDate..: 01/01/1970
Today....: 02/01/2026
DaysSince: 20455

OldDate..: 31/12/1969
Today....: 02/01/2026
DaysSince: 0

kofa kofa

unread,
Jan 2, 2026, 12:26:45 PMJan 2
to The Ring Programming Language

Untitled.jpg

Mahmoud Fayed

unread,
Jan 2, 2026, 12:46:27 PMJan 2
to The Ring Programming Language
Hello Bert, Kofo

Diffdays() is based on mktime() C function
On Windows, the range is 1970-2038

So, please QDate as in the next example

diffdays.png

Source Code

load "guilib.ring"


d1 = new QDate() { setDate(2026,01,02) }

? d1.day()

? d1.month()

? d1.year()


d2 = new QDate() { setDate(1922,04,20) }

? d2.day()

? d2.month()

? d2.year()


? d1.daysto(d2)


Greetings,

Mahmoud

kofa kofa

unread,
Jan 2, 2026, 1:19:00 PMJan 2
to The Ring Programming Language
thank you very much
Untitled.jpg

Mahmoud Fayed

unread,
Jan 2, 2026, 1:22:06 PMJan 2
to The Ring Programming Language
Hello Kofa

You are welcome :D

Greetings,
Mahmoud

Bert Mariani

unread,
Jan 2, 2026, 1:24:48 PMJan 2
to The Ring Programming Language
An Alternative using  EpochTime()

========================
load "stdlibcore.ring"
// EpochTime(Date(), Time() )  // return EpochSec

CurSec  = EpochTime(Date(), "00:00:00" )  
OldSec  = EpochTime("20/04/1922", "00:00:00" )
DiffSec = CurSec - OldSec

See "CurSec: "+ CurSec +nl
See "OldSec: "+ OldSec +nl

NbrDays = DiffSec / 86400
See "NbrDays: "+ NbrDays +nl

=========================

CurSec: 1767312000        
OldSec: -1514764800       
NbrDays: 37987

========================

kofa kofa

unread,
Jan 2, 2026, 1:31:49 PMJan 2
to The Ring Programming Language
the result not change if you change  for example : 10/10/1922

kofa kofa

unread,
Jan 2, 2026, 1:33:41 PMJan 2
to The Ring Programming Language
Untitled.jpg

Mahmoud Fayed

unread,
Jan 2, 2026, 1:40:53 PMJan 2
to The Ring Programming Language
Hello Kofa, Bert

EpochTime() implementation uses DiffDays()
So, it's better to use QDate to avoid the (1970 to 2038 limitation on Windows)

Greetings,
Mahmoud

Mahmoud Fayed

unread,
Jan 2, 2026, 1:50:41 PMJan 2
to The Ring Programming Language
Hello Bert

>> "OldSec  = EpochTime("20/04/1922", "00:00:00" )"

Started from Ring 1.25 this will produce an error message on Windows (by DiffDays() called from EpochTime())

As a result of this update (done today), where we detect this error: Update Visual Source (Using PWCT) - Revise diffdays() implementation · ring-lang/ring@81af280
By checking if vTimer == -1

Greetings,
Mahmoud

kofa kofa

unread,
Jan 2, 2026, 1:54:13 PMJan 2
to The Ring Programming Language
thanks

Mahmoud Fayed

unread,
Jan 2, 2026, 2:23:59 PMJan 2
to The Ring Programming Language
Hello Kofa

You are welcome :D

Greetings,
Mahmoud

Bert Mariani

unread,
Jan 2, 2026, 9:16:58 PMJan 2
to The Ring Programming Language
Hello Mahmoud , Kofa

Another method
Using Astronomical days based on Julian Period
The Julian period is a chronological interval of 7980 years

### Astronomers refer to a Julian date as the number of days
### since the beginning of the Julian Period (January 1, 4713 BC at 12:00 Noon).
###
### 2026-01-02 = 2,461,043 days  
### 1922-04-20 = 2,423,165 days
### Difference =    37,878 days

========================
load "stdlibcore.ring"

CurYYYY = 2026  CurMM = 01  CurDD = 02
OldYYYY = 1922  OldMM = 04  OldDD = 20

? "CurDate: " + CurYYYY +","+ CurMM +","+ CurDD
? "OldDate: " + OldYYYY +","+ OldMM +","+ OldDD
? ""

CurJulian = JulianDay(CurYYYY,CurMM,CurDD)
OldJulian = JulianDay(OldYYYY,OldMM,OldDD)

NbrDays   = CurJulian - OldJulian

? "CurJulian: " + CurJulian
? "OldJulian: " + OldJulian
? "NbrDays..: " + NbrDays


# ---------- Time / Calendar ----------
func JulianDay(y,m,d)
    yy = y
    mm = m
    if mm <= 2
        yy = yy - 1
        mm = mm + 12
    end
    A = floor(yy/100)
    B = 2 - A + floor(A/4)
    jd = floor(365.25*(yy + 4716)) + floor(30.6001*(mm + 1)) + d + B - 1524.5

    return jd
end

# =================================
OUTPUT

CurDate: 2026,1,2
OldDate: 1922,4,20

CurJulian: 2461042.50
OldJulian: 2423164.50
NbrDays..: 37878

Julian-Diff-Days-1.ring

Mahmoud Fayed

unread,
Jan 3, 2026, 11:33:18 AMJan 3
to The Ring Programming Language
Hello Bert

Thanks for sharing :D

Greetings,
Mahmoud

kofa kofa

unread,
Jan 4, 2026, 12:09:13 PMJan 4
to The Ring Programming Language
thank youUntitled.jpg
Reply all
Reply to author
Forward
0 new messages