Author Topic: How to calculate days remaining?  (Read 3941 times)

0 Members and 1 Guest are viewing this topic.

Sdoman

  • Guest
How to calculate days remaining?
« on: June 02, 2006, 11:59:40 AM »
Q:  How can I determine how many days remain between a given date and the current date?

I've been using (getvar "cdate") to get the current date.  Subtracting the current date from the target date yeilds a weird number.  Maybe I don't understand the format.

If the target date is June 9th 2006, then :
Code: [Select]
(- 20060609.0100 (getvar "cdate"))
-> 6.92418

Doesn't make sense to me.

Bob Wahr

  • Guest
Re: How to calculate days remaining?
« Reply #1 on: June 02, 2006, 12:09:44 PM »
Six days and change.  Sounds about right to me.  Where you are running into problems though is that it's not decimal numbers that you're using, it's yyyymmdd.hhmmssdd so if you want the difference down to the time you will have to subtract date first, then time.  There's possibly a lisp function for it but I have no idea.

Sdoman

  • Guest
Re: How to calculate days remaining?
« Reply #2 on: June 02, 2006, 12:11:08 PM »
Oh so it's days not hours.  Thanks Bob.

Sdoman

  • Guest
Re: How to calculate days remaining?
« Reply #3 on: June 02, 2006, 12:23:03 PM »
I'm still confused

Code: [Select]
(- 20060702.0100 (getvar "cdate"))
-> 99.9178

Should be 30 days  :ugly:

LE

  • Guest
Re: How to calculate days remaining?
« Reply #4 on: June 02, 2006, 12:35:58 PM »
Hello, here is a very old topic, that maybe, could help

http://discussion.autodesk.com/thread.jspa?messageID=1225491

Bob Wahr

  • Guest
Re: How to calculate days remaining?
« Reply #5 on: June 02, 2006, 12:38:42 PM »
If your formula took into account that you were working with dates.  It doesn't.  What you are telling it is that you want the result for:

 20060702.0100
-20060602.0922
            99.9178

here's how it could be done in VBA someone else will have to supply the lisp
Code: [Select]
Sub test()
Dim vardate As Variant
Dim lngDif as Long
vardate = #6/9/2006 1:00:00 AM#
lngDif = DateDiff("n", Now, vardate)
End Sub
The above will return the difference in minutes.

CADaver

  • Guest
Re: How to calculate days remaining?
« Reply #6 on: June 02, 2006, 12:44:23 PM »
Simple subtraction won't work because the number is not a real number, but fabricated out of YEARMODY.HRMNSEC

So last While last Wednesday was:
20060531.010000
and yesterday was:
20060601.010000

The two are only a day apart, but 0531 from 0601 is 70.


Instead of CDATE, investigate DATE

http://aa.usno.navy.mil/data/docs/JulianDate.html

« Last Edit: June 02, 2006, 12:52:25 PM by CADaver »

Sdoman

  • Guest
Re: How to calculate days remaining?
« Reply #7 on: June 02, 2006, 01:12:30 PM »
Got it.  Thanks everyone!

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to calculate days remaining?
« Reply #8 on: June 02, 2006, 05:02:29 PM »
Look in Express tools folder for the julian.lsp. There you will find the (defun CTOJ function.
Feed it (CTOJ <year> <month> <day>  0 0 0) and it will return the julian date.

you can use it like this:
Code: [Select]
(defun c:test ()
  ;;    Month Day Year
  (print (DaysBetween 1 1 2006 1 2 2006))
  (print (DayOfYear 12 31 2006))
  (princ)
)

(defun DaysBetween (m1 d1 y1 m2 d2 y2)
  (fix (- (ctoj y2 m2 d2 0 0 0) (ctoj y1 m1 d1 0 0 0)))
)

(defun dayOfYear (m d y / year)
  (if (= y (setq year (fix(/(getvar "CDATE") 10000))))
     (1+ (fix (- (ctoj y m d 0 0 0) (ctoj year 1 1 0 0 0)))))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Sdoman

  • Guest
Re: How to calculate days remaining?
« Reply #9 on: June 03, 2006, 09:57:01 AM »
CAB,

Looks like your suggestion to use the CTOJ function will work very well for my purpose.  I've been busy and had to postpone this programming task, but I think I'll have time tonight to try.

My initial mistake was thinking that the real number returned from the "CDATE" variable could be subtracted to get time differences.  Thanks to all for clarification on that misconception.

Thanks a billion!