Elisp: Date Time

By Xah Lee. Date: . Last updated: .

This page shows you how to print current date time in various formats.

If you want to parse date/time, see: Elisp: Parsing Date Time.

For a ready-to-use command to insert date time, see Emacs: Insert Date Time

Format yyyy-mm-dd

If you want the format to be yyyy-mm-dd, do (format-time-string "%Y-%m-%d")

(format-time-string "%Y-%m-%d")
;; "2018-09-10"

ISO 8601 Format

To use ISO 8601 format, e.g. 2018-09-10T17:39:31-07:00. Code it like this:

(concat
 (format-time-string "%Y-%m-%dT%T")
 ((lambda (x) (concat (substring x 0 3) ":" (substring x 3 5)))
  (format-time-string "%z")))

;; sample output
;; 2018-09-10T17:39:31-07:00

Unix Time Format

To print Unix time format (i.e. number of seconds since .), use %s. Like this:

;; unix time
(format-time-string "%s") ; "1291104066"

Names for Month and Week

You can also print names for month and week, both full name or abbreviation.

;; full month name
(format-time-string "%B") ; "November"

;; abbreviated month name
(format-time-string "%b") ; "Nov"
;; full week name
(format-time-string "%A") ; "Tuesday"

;; abbreviated week name
(format-time-string "%a") ; "Tue"

Ordinal Date Format

format-time-string also supports ordinal date format. For example:

(format-time-string "%Y-%j") ; "2010-334" for 2010-11-30

(info "(elisp) Time Parsing")

(info "(elisp) Time of Day")

ELisp Date Time

  1. Elisp: Date Time
  2. Elisp: Parsing Date Time
  3. Emacs: Insert Date Time
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.