Reputation: 401
I read the date from a file to a variable. The date has the format ddmmyyyy. It has to be converted to yyyy-mm-dd I already searched this forum and got this far : date -d '$DATE' +%F The problem is the input format is not recognised. Is there any way I can specify the input date format? On an other forum I found : date -d "${OLD_DATE}" -D "%d%m%Y" +%F where -D should specify the input format but this doesn't work. But -D is unknown.
thanks for the help and sorry for my English.
Upvotes: 1
Views: 2020
Reputation: 65791
Yes, date
understands a lot of formats for -d
, but when it's just 8 digits in a row, it interprets it as YYYYmmdd
. I'm not sure if you can force it to read it differently, but in this case you can use a simple editor such as awk
or sed
instead:
$ OLD_DATE='08032011'
$ echo $OLD_DATE | sed -r 's/(.{2})(.{2})(.{4})/\3-\2-\1/'
2011-03-08
This will work on GNU sed
. Note that it doesn't check the input (for brevity).
Upvotes: 1
Reputation: 2454
You could to it like this:
echo "DDMMYYYY" | awk 'BEGIN {OFS="-"} {print substr($1,5,4), substr($1,3,2), substr($1,1,2)}'
Output:
YYYY-MM-DD
Upvotes: 1