Getting date yyyymmdd format in Shell Script is something tricky. I have spent almost 15 minutes to get this format, this may be helpful for someone to save their time.
1. Date YYYYMMDD format in Shell Script:
The syntax for getting today’s date in yyyymmdd format in the shell script.
$date +%Y%m%d
20190928 -- output
2. The Day Before Dates in YYYYMMDD format:
We can also send parameter to date command to get the day before dates, for instance, yesterday, the day before yesterday and so on.
#Yesterday date
$date --date="1 days ago" +%Y%m%d
20190927 --output
#2 days before date
$date --date="2 days ago" +%Y%m%d
20190926 --output
3. Shell Script:
date_format.sh
#!/bin/bash
FORMATE_DATE='date --date="2 days ago" +%Y%m%d'
echo "The day before yesterday - ${FORMATE_DATE}"
Output:
Terminal
$./date_format.sh
The day before yesterday - 20190926
Done!
Happy Learning 🙂