`parse-time-string` takes a time string and parses the constituents of the date/time string:
(parse-time-string "Sunday, 12 June 2022")
(nil nil nil 12 6 2022 0 -1 nil)
Now is there a way to format this into a string again?
I could take each part and generate a new string, but there might be a function that I am not aware of?
## Edit: My solution for this
;; (parse-time-string STRING) return a list like this:
;; (SEC MIN HOUR DAY MON YEAR DOW DST TZ)
(defun ugt-convert-time-string (time-string)
"Converts TIME-STRING into one with this format:
yyyy-mm-dd Day"
(cl-flet ((get-day (dow)
(pcase dow
(0 "Sun")
(1 "Mon")
(2 "Tue")
(3 "Wed")
(4 "Thu")
(5 "Fri")
(6 "Sat"))))
(pcase-let ((`(,sec ,min ,hour ,day ,mon ,year ,dow ,dst ,tz)
(parse-time-string time-string)))
(format "%04d-%02d-%02d %s" year mon day (get-day dow)))))
(ugt-convert-time-string "Sunday, 12 June 2022")
"2022-06-12 Sun"
## Edit 2: Here's a better solution with the help of u/flyin1501
;;;;; convert marked (arbitrary) date string into canonical form yyyy-dd-mm Wkd HH:MM:SS
;; `encode-time'
;; TIME is a list (SEC MIN HOUR DAY MON YEAR IGNORED DST ZONE).
;; `parse-time-string'
;; Parse the time in STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
(defun ugt-convert-timestring-to-canonical (rbegin rend)
"Convert arbitrary time-string into `yyyy-mm-dd Wkd'."
(interactive "r")
(if (use-region-p)
(pcase-let* ((`,time-string (buffer-substring-no-properties rbegin rend))
;; unpack list elements
(`(,sec ,min ,hour ,day ,mon ,year ,dow ,dst ,tz) (parse-time-string time-string)))
;; reconstruct time-list with TZ=nil
(let* ((time-list (list
(or sec 0)
(or min 0)
(or hour 0)
day
mon
year
dow
dst
nil ;; add `nil' to end, as TZ needs to be `nil'
))
(new-time-string (format-time-string "[%F %a %H:%M:%S]"
(encode-time time-list))))
(kill-new new-time-string)
(message "Converted %s to %s" time-string new-time-string)))))