user9903
user9903

Reputation:

How do I add dates in Emacs using Emacs Lisp?

I want to use Emacs Lisp to perform math operations like add and difference on dates and times.

Upvotes: 11

Views: 4998

Answers (1)

mch
mch

Reputation: 7393

The short answer is: read the System Interface section of the elisp info manual. More specifically, the time sections:

The longer answer:

Emacs can work with time values as strings, floats, or a tuple of two or three integers. E.g. call some functions in the *scratch*buffer:

(current-time)
(19689 59702 984160)
(current-time-string)
"Sun Nov 21 20:54:22 2010"
(current-time-zone)
(-25200 "MST")
(float-time)
1290398079.965001

Let's some conversions:

(decode-time (current-time))
(33 7 21 21 11 2010 0 nil -25200)
(decode-time) ; (current-time) by default
(51 7 21 21 11 2010 0 nil -25200)

(let ((seconds 36)
      (minutes 10)
      (hour 21)
      (day 21)
      (month 11)
      (year 2010))
  (encode-time seconds minutes hour day month year))
(19689 60732)

(format-time-string "%A %e %B" (current-time))
"Sunday 21 November"

(seconds-to-time 23)
(0 23 0)

(time-to-seconds (current-time))
1290399309.598342

(time-to-days (current-time))
734097

Finally, to answer your question:

(time-add (current-time) (seconds-to-time 23))
(19689 60954 497526)

(time-subtract (current-time) (seconds-to-time 45))
(19689 61001 736330)

Upvotes: 23

Related Questions