4

I was not very statisfied with the leading zero that I got in my insert-date function.

It looks now as 09 jun 2015, instead 9 jun 2015.

To solve this, I looked into the source code of s.el and wrote a function for it.

(defun insert-date ()
  "Insert current date 1 jun yyyy."
  (interactive)
  (insert
   (if (string-prefix-p "0" (format-time-string "%d"))   ;; Checks if there is a leding zero. 
       (s-right (format-time-string "%d") 1)             ;; If so, chop it off with the library s, then s-right. 
     (format-time-string "%d"))                          ;; If not (for example, 10, 12, 25, 31), let it so. 
   (format-time-string "%b %Y"))                         ;; Then the rest (jun 2015). 
  ) 

After some tweaking, I still got the same error that there is a wrong type argument in sequencep 1. But I couldn't figure out.

Anybody have the golden hint?

5
  • 1
    What not use %e instead of %d? As per the documentation for format-time-string: "%d is the day of the month, zero-padded, %e is blank-padded." And if you don't want that blank you can send it to string-trim. Also, if you look at the doc for s-right, the function signature looks like this: (s-right LEN S). You have it swapped.
    – nanny
    Jun 9, 2015 at 20:15
  • 1
    @nanny I'm aware of %e but it adds an extra space, which I don't prefer. So foo-bar (01-06-2015) will be foo-bar ( 1-06-2015). Never knew I could send it to string-trim, but I need to check if there is any space. Thanks for the suggestion. And it seems I have swapped it, indeed. Your answer was the right, it worked again.
    – ReneFroger
    Jun 9, 2015 at 20:19
  • 1
    I made an edit: you can always use (string-trim (format-time-string "%e %b %Y")).
    – nanny
    Jun 9, 2015 at 20:22
  • 1
    @nanny Thanks, that's even a better solution. I wished that you added it as an answer, so I could validate your answer as the right one. Thanks!
    – ReneFroger
    Jun 9, 2015 at 20:28
  • You're welcome! Not a problem, just accept your own answer as the correct one.
    – nanny
    Jun 9, 2015 at 20:36

2 Answers 2

5

If you look in the documentation for #'format-time-string (try C-h f format-time-string), it explains %d and %e.

%d is the day of the month, zero-padded, %e is blank-padded.

But it also goes on to say:

Certain flags and modifiers are available with some format controls.
The flags are `_', `-', `^' and `#'.  For certain characters X,
%_X is like %X, but padded with blanks; %-X is like %X,
but without padding.

So let's try it!

(let ((time (date-to-time "June 9 12:00 2015")))
  (format-time-string "%-e %b %Y" time))

results in 9 Jun 2015, with no padding at all.

The flag - is easier than trimming the string, as it also works if you want to use a numeric value inside the string, for example:

(let ((time (date-to-time "June 9 12:00 2015")))
  (format-time-string "%-e/%-m/%Y" time))

results in 9/6/2015.

1
  • Even a better answer, thanks for that! I must looking up the documentation more often.
    – ReneFroger
    Jun 9, 2015 at 22:16
0

The user nanny solved it with

(string-trim (format-time-string "%e %b %Y"))

I didn't wanted the %e becauses it added a space. But I never knew the function string-trim chops the spaces off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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