Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting current system date in Common Lisp

I am trying to get the current system date in Common Lisp through following function

(defun current-date-string ()
  "Returns current date as a string."
  (multiple-value-bind (sec min hr day mon yr dow dst-p tz)
                       (get-decoded-time)
    (declare (ignore sec min hr dow dst-p tz))
    (format nil "~A-~A-~A" yr mon day)))

Unfortunately I am getting the current date in this format "2014-1-2". However actually I need this format "2014-01-02". Is any way we can change the format? I tried replacing nil with yyyy-mm-dd but no luck. However my machine clock shows the date format is "2014-01-02".

like image 740
user3061324 Avatar asked Feb 14 '23 06:02

user3061324


1 Answers

What you need is

(format nil "~4,'0d-~2,'0d-~2,'0d" yr mon day)

~2,'0d means:

  • d: decimal output (instead of your generic a)
  • 2: 1st argument: width
  • '0: 2nd argument: pad char 0

I suggest that you read up on Formatted Output; format is a very powerful tool.

like image 152
sds Avatar answered Mar 15 '23 02:03

sds