I want to convert it from
Format 1: 12272019(this is just a number)
To
Format 2: 2019-12-27
I tried using below: date -d '12272019' +%Y-%m-%d
But it is showing invalid date format
I want to convert it from
Format 1: 12272019(this is just a number)
To
Format 2: 2019-12-27
I tried using below: date -d '12272019' +%Y-%m-%d
But it is showing invalid date format
The date command only accepts a predefined set of formats for its input. Your format mmddyyyy is not part of that set.
You can re-arrange your date string manually using either sed:
sed -E 's/(..)(..)(....)/\3-\1-\2/' <<< 12272019 or bash:
date=12272019 echo "${date:4}-${date:0:2}-${date:2:2}" On OS/X, date is quite a bit different, so you can specify the input format for date:
date -jf "%m%d%Y" 12272019 +"%Y-%m-%d" # => 2019-12-27 Linux does not allow you to do that though, but you can do it easily with sed:
echo 12272019 | sed -e 's/\(..\)\(..\)\(....\)/\3-\1-\2/' # => 2019-12-27