TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: @_Bilal on January 19, 2019, 07:35:15 AM

Title: Deduct Dates in Autolisp
Post by: @_Bilal on January 19, 2019, 07:35:15 AM
Hi Guys,

Is there an AutoLISP routine to deduct two dates and get the results in days
for example: last date is  "19750225"  year 1975 month 02 day 25 and the current date is (rtos (getvar "cdate") 2 0)
The question is how to get the exact days between this 2 dates in autolisp.

Thanks
Title: Re: Deduct Dates in Autolisp
Post by: Lee Mac on January 19, 2019, 08:01:51 AM
Convert the dates to Julian then subtract, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun daydiff ( d1 d2 )
  2.     (- (fix (dtoj d2)) (fix (dtoj d1)))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (daydiff 20190117 (getvar 'cdate))
  2. 2
  3. _$ (daydiff 20181219 (getvar 'cdate))
  4. 31

(dtoj is part of Julian.lsp included with Express Tools)

Note that the DATE system variable returns a Julian date already, so if you simply want the difference from the current date, the code may be:
Code - Auto/Visual Lisp: [Select]
  1. (defun daydiffnow ( d1 )
  2.     (- (fix (getvar 'date)) (fix (dtoj d1)))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (daydiffnow 20190117)
  2. 2
Title: Re: Deduct Dates in Autolisp
Post by: @_Bilal on January 19, 2019, 08:22:46 AM
Thank you Lee Mac.