[Shell Script] Getting formatted dates for yesterday, today, and tomorrow

Tadashi Shigeoka ·  Wed, June 22, 2016

I’ll introduce how to get formatted dates for “yesterday,” “today,” and “tomorrow” in shell scripts, formatted for easy use in Google Spreadsheet and MongoDB queries.

Premise

“Today’s” date is 2016/06/22.

Getting dates for yesterday, today, and tomorrow

# Yesterday's date
% date -d '1 days ago'
Tue Jun 21 13:40:41 UTC 2016
# Today's date
% date
Wed Jun 22 13:40:42 UTC 2016
# Tomorrow's date
% date -d '1 days'
Thu Jun 23 13:40:43 UTC 2016

Getting dates for yesterday, today, and tomorrow (formatted version)

I output each date with hyphen separators to make them easier to handle in Google Spreadsheet and MongoDB queries.

# Yesterday's date
% date '+%Y-%m-%d' -d '1 days ago'
2016-06-21
# Today's date
% date '+%Y-%m-%d'
2016-06-22
# Tomorrow's date
% date '+%Y-%m-%d' -d '1 days'
2016-06-23

That’s all.

Reference Information

That’s all from the Gemba.