Linux

Date yyyymmdd format in Shell Script

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 :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

1 comment

Ask a question or leave a comment

Our product

Preparing for an AWS, Azure or Google Cloud exam?

Take a free 20-question sample test with an explanation for every answer. No sign-up needed.

Start the free test