4

(format-time-string "%p") is empty in my Emacs. How I can format date in AM/PM format?

That answer second part of Show time in different time zones

UPDATE

(length (format-time-string "%p"))  ; is 0
(getenv "LC_TIME")                  ; is "en_DK.utf8"

My default preferences (in .emacs):

(setq display-time-24hr-format t)
(setq display-time-day-and-date nil)
  • The documentation says that %p is the locale’s equivalent of either AM or PM. So I suspect the problem is with the en_DK locale on your machine. You could try the command date +%p in a terminal window. Does it match what you see in emacs? (Make sure that the locale matches when you test.) – Harald Hanche-Olsen Mar 8 '16 at 16:40
2

format-time-string requires a format together with an actual time to format. So you want to write something like

(format-time-string "%p" (current-time))

which at the moment outputs am for me. Similarly

(format-time-string "%l:%M %p" (current-time))

currently gives 9:40 am.

0

Inside the emacs manual there is this page: https://www.gnu.org/software/emacs/manual/html_node/emacs/Time-Display-Format.html. that talks about changing the time format.

The following code should do what you want if I understand correctly.

(setq display-time-24hr-format nil)
(setq display-time-string-forms
      '(12-hours ":" minutes am-pm))
  • How your suggestion can be applied to (format-time-string "%p") without altering global settings? – gavenkoa Mar 7 '16 at 16:38
  • Well I guess I don't know exactly what you mean by it being empty. Would you be able to add something like this: (format-time-string (concat "Start of string " (if (>= (string-to-number (substring (current-time-string (current-time)) 11 13)) 12) "PM" "AM") " end of string")) – Jules Mar 7 '16 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.