When invoked without arguments, the date command displays the current date and time. Depending on the options specified, the date would set the date and time or print it in a user-defined format. However, how do you get yesterdays with bash? What about tomorrows day? I have seen many sysadmins writing Perl scripts for calculating relative dates such as yesterdays or tomorrows day. You can use the GNU date command, which is designed to handle relative date calculation such as:
- 1 Year
- 2 Days
- 2 Days ago
- 5 Years
Getting Yesterdays or Tomorrows Day with bash shell date command
Let us see syntax and sample examples.
GNU date syntax
The syntax is as follows:
date --date="STRING" date --date="next Friday" date --date="2 days ago" |
The --date=STRING is a human-readable format such as “next Thursday” or “1 month ago”. A date string may contain items indicating calendar date, time of day, time zone, day of the week, relative time, relative date, and numbers.
Why use relative GNU/date formats?
- Ease of use
- Write your shell scripts
- Automate task using Linux cron (example run a job on the last day of the month or Nth day of the month or 3rd Friday and so on)
Examples
First, try to display today’s date, enter:$ date
Sample outputs:
Wed Jun 15 04:47:45 IST 2011
To display yesterday’s date, enter:$ date --date="1 days ago"
$ date --date="1 day ago"
$ date --date="yesterday"
$ date --date="-1 day"
Sample outputs:
Tue Jun 14 04:54:40 IST 2011
You can use various string formats to produce the same output. Please note that the output of the date command is not always acceptable as a date string, not only because of the language problem but also because there is no standard meaning for time zone items like IST or EST.
Find tomorrow’s date with date command
Type the following command$ date --date="-1 days ago"
Or$ date --date="next day"
Getting date in the future
To get tomorrow and day after tomorrow (tomorrow+N) use day word to get date in the future as follows:
date --date='tomorrow' date --date='1 day' date --date='10 day' date --date='10 week' date --date='10 month' date --date='10 year' |
The date string ‘tomorrow’ is worth one day in the future which is equivalent to ‘day’ string i.e. first two commands are same.
Getting Date In the Past
To get yesterday and earlier day in the past use string day ago:
date --date='yesterday' date --date='1 day ago' date --date='10 day ago' date --date='10 week ago' date --date='10 month ago' date --date='10 year ago' |
The date string ‘yesterday’ is worth one day in the past which is equivalent to ‘day ago’ string i.e. first two commands are same.
Moving by whole years or months
You can add year and months keywords to get more accurate date:$ date --date='2 year ago' # Past
$ date --date='3 years' # Go into future
$ date --date='2 days' # Future
$ date --date='1 month ago' # Past
$ date --date='2 months' # Future
Moving Date Using More Precise Units
- You can use fortnight for 14 day.
- week for 7 days.
- hour for 60 minutes
- minute for 60 seconds
- second for one second
- You can also use this / now / today keywords to stress the meaning.
Few examples using precise string units:
date --date='fortnight' date --date='5 fortnight' date --date='fortnight ago' date --date='5 fortnight ago' date --date='2 hour' date --date='2 hour ago' date --date='20 minute' date --date='20 minute ago' |
Moving Date Using the Day of Week Items
To print the date of this Friday, enter:
date --date='this Friday' ## OR ## date --date='next Friday' |
Days of the week may be spelled out in full: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday or Saturday. Days may be abbreviated to their first three letters, optionally followed by a period
date --date='this Fri' ## OR ## date --date='next Fri.' |
You can also move forward supplementary weeks as follow:
date --date='2 Fri' ## OR ## date --date='second Fri.' ## OR ## date --date='Second Friday' #### #### last DAY or next DAY move one week before or after the day that DAY by itself #### date --date='last Friday' date --date='next Friday' |
To print the date in the future ($now + 6 months + 15 days), enter:$ date --date='6 months 15 day'
To print the date in the past [$now – (two months and 5 days ago) ], enter:$ date --date='-2 months 5 day ago'
Display Date Using Epoch Time
To display date in epoch time:$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s
How Do I Use Relative Date Format To Set System Date & Time?
You can also use relative format to setup date and time. For example to set the system clock forward by 30 minutes, enter (you must be login as root to set the system date and time):# date --set='+30 minutes'
OR# date --set='1 day ago'
OR# date --set='5 day'
However, I recommend setting NTPD client / server or OpenNTPD server to synchronize the local clock of a computer system with remote NTP servers.
How Do I Assigned Yesterday To Shell Variable?
Use the following syntax (see assign values to shell variables and command substitution for more information)
yest=$(date --date="yesterday") echo "$yest" yest=$(date --date="yesterday" +"%d/%m/%Y") echo "The backup was last verified on $yest" |
Say you set date format as follows:MY_DATE=JULY-18-2019
To get the first day of the given day’s month from $MY_DATE, run:date -d "$MY_DATE" +%b-01-%Y
Previous date of the given date’s month from $MY_DATE:date -d "$MY_DATE -1 days" +%b-%d-%Y
Get the last day of the month:t_FIRST_DAY_NEXT_MON=$(date -d "$MY_DATE +1 month" +%b-01-%Y)
echo "$t_FIRST_DAY_NEXT_MON"
date -d "$t_FIRST_DAY_NEXT_MON -1 day" +%b-%d-%Y
Conclusion
This page explained the date command and how to use to get relative dates as per your needs on Linux or Unix-like systems. For more info see:
- man date
- How To Format Date For Display or Use In a Shell Script
- GNU/date documentation here