Issue
I know the topic has already emerged and some of the posts give a good summary like the one here: Convert string to date in bash . Nevertheless, I encounter a problem presented below with an example I should solve:
date +'%d.%m.%y'
works as desired and returns 05.12.20
but the inverse operation I should use to convert strings to date fails:
date -d "05.12.20" +'%d.%m.%y'
date: invalid date ‘05.12.20’
and this is exactly what I need. The Unix date formatting I have also checked on https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/ but it seems to be in line with that. What is the problem? I also tried to supply time zone indicators like CEST but they did not solve the problem.
Solution
Try
date -d "05-12-20" +'%d.%m.%y'
UNIX date
expects either -
or /
as a date separator.
However, if your input really must be in the format "05.12.20" (i.e. using .
), then you can convert it to the format expected by UNIX date
:
date -d `echo "05.12.20" | sed 's/\./-/g'` +'%d.%m.%y'
Answered By - costaparas