Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Date() + one year

100 views
Skip to first unread message

Otto Haldi

unread,
Oct 31, 2022, 10:32:19 AM10/31/22
to
Hello,
What is the easiest way to add a year to a date?
If I do Date()+365, I will have an error with leap years.

Enrico Maria Giordano

unread,
Oct 31, 2022, 10:50:57 AM10/31/22
to
Something like

Date() + 365 + IF( IsLeap( Year( Date() ) ), 1, 0 )

--
Enrico Maria Giordano

http://www.emagsoftware.it
http://www.emagsoftware.it/emgmusic
http://www.emagsoftware.it/spectrum
http://www.emagsoftware.it/tbosg

dlzc

unread,
Oct 31, 2022, 11:58:11 AM10/31/22
to
You just need to increment the year. Enrico's method fails if "next year" is a leap year and the date sought is after February. This is sensitive to the date format you have set...

dThisOne := date()
dThatOne := CTOD( str( month( dThisOne )) + "-" + ;
str(day( dThisOne )) + "-" +;
str(year( dThisOne + 1 )) )

Maybe something safer derived from Clipper Tools using STOD and PADLEFT...

dThisOne := date()
dThatOne := STOD( PADLEFT( str( year( dThisOne + 1 )), "0") +;
PADLEFT(str( month( dThisOne )), "0") +;
PADLEFT(str( day( dThisOne )), "0") );

Coded in my head, not tried.

David A. Smith

Enrico Maria Giordano

unread,
Oct 31, 2022, 1:47:59 PM10/31/22
to
Il 31/10/2022 16:58, dlzc ha scritto:

> Enrico's method fails if "next year" is a leap year and the date sought is after February.

Ops, you are right!

Rainer Berning

unread,
Oct 31, 2022, 2:58:33 PM10/31/22
to
Am 31.10.2022 um 18:47 schrieb Enrico Maria Giordano:
> Il 31/10/2022 16:58, dlzc ha scritto:
>
>> Enrico's method fails if "next year" is a leap year and the date sought is after February.
>
> Ops, you are right!
>

Use function AddMonth from xHarbour.

-> nDate := AddMonth(Date(), 12)

R. Berning


Otto Haldi

unread,
Nov 1, 2022, 9:03:57 AM11/1/22
to
Thanks so much that fixed the problem, you help is appreciated
Otto

timepro timesheet

unread,
Nov 1, 2022, 10:16:57 AM11/1/22
to
hi:

i have this simple method:

if (year(yourdate+nnn))/4=int((year(yourdate+nnn))/4)
'this is leap year'
...
...

Ingo Steinbuechel

unread,
Nov 1, 2022, 12:37:06 PM11/1/22
to
Hi,

"timepro timesheet" <timec...@gmail.com> schrieb:

> i have this simple method:
>
> if (year(yourdate+nnn))/4=int((year(yourdate+nnn))/4)
> 'this is leap year'

this is not correct. Look at the definition of a leap year [1].

Regards Ingo

[1] https://en.wikipedia.org/wiki/Leap_year#Algorithm

--
Threema - Sicherer und privater Messenger: https://threema.ch/de
Meine Threema-ID: https://threema.id/ZV9BWDXK
Warum Threema? https://warumthreema.de/

timepro timesheet

unread,
Nov 1, 2022, 1:07:26 PM11/1/22
to
ingo:

by algorithm/mathematically...you are right...
(2100 can be divided by 4, but isn't a leap year)
but does anyone contemplate (while) coding for year 2100 onward...(the 'next century' leap year should be 2400).

Enrico Maria Giordano

unread,
Nov 1, 2022, 1:46:19 PM11/1/22
to
Il 01/11/2022 18:07, timepro timesheet ha scritto:

> but does anyone contemplate (while) coding for year 2100 onward...

Of course! :-)

dlzc

unread,
Nov 3, 2022, 2:57:00 PM11/3/22
to
Elegant. According to Clipper Tools, could be:
-> nDate := AddMonth( , 12) (or maybe just ->AddMonth(12) )
... since it says it defaults to system date

David A. Smith
0 new messages