Here is code that I use for a format-time-string for a fake calendar:
(defun mystcal-format-time (format-string time)
%Y is the year.
%m is the numeric month.
%B is the full name of the month.
%d is the day of the month, zero-padded, %e is blank-padded.
%u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.
%A is the locale's full name of the day of week.
%H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H
only blank-padded, %l is like %I blank-padded.
%p is the locale's equivalent of either AM or PM.
%M is the minute.
%S is the second."
(decoded-time (if (listp time)
(turn (nth 0 decoded-time))
(minute (nth 1 decoded-time))
(hour (nth 2 decoded-time))
(day (nth 3 decoded-time))
(month (mystcal-month decoded-time))
(year (nth 5 decoded-time))
(weekday (mystcal-weekday-of-day day))
(hour12 (mod hour 12)))
(while (string-match "%" format-string start)
(let ((index (match-beginning 0)))
;; First copy non-format text
(setq output (concat output (substring format-string start
;; Process format codes here
(setq output (concat output
(case (aref format-string (1+ index))
(?Y (number-to-string year))
(?m (number-to-string month))
(?B (mystcal-month-name month))
(?d (format "%02d" day))
(?e (format "%2d" day))
(?u (number-to-string (if (zerop weekday)
(?w (number-to-string weekday))
(?H (format "%02d" hour))
(?k (format "%2d" hour))
(?I (format "%02d" (if (zerop hour12) 12 hour12)))
(?l (format "%2d" (if (zerop hour12) 12 hour12)))
(?p (if (< hour 12)
(?M (format "%02d" minute))
(setq start (+ 2 index)))))
(setq output (concat output (substring format-string start)))