How to add days to date and get new date on Linux

I brought eggs from the market, and it says best before 20 days from the date of packing. Say my egg box was packed on 29/July/2020. How do I add days to date using Linux CLI? I want to find out date 29/July/2020 + 20days using the CLI, and how do I do it?

We use the date command to display the current date and time on Linux or Unix-like operating systems. Similarly, you can set the date and time using the date command on Linux. Let us see how to use the date command to add days to a given date/period.

ADVERTISEMENTS

How to add days to date and get new date on Linux

The syntax to display the current time and date is as follows for GNU/Linux version of date (macOS/FreeBSD/*BSD users jump below):
date
Display the time on the west coast of the US using TZ without changing local time:
TZ='America/Los_Angeles' date
Here is another example for the local time for 6AM next Thu on the west coast of the US:
date --date='TZ="America/Los_Angeles" 06:00 next Thu'
We can show time described by the --date or -d option when using GNU date version. Say you want to print the date of the day before yesterday, you would type:
date --date='2 days ago'
Let us see the date of the day four months and two day hence:
date --date='4 months 2 day'

Adding days to date command

The syntax is as follows to add days to given date and find new date/time on GNU/Linux date version:

date -d="YYYY-MM-DD+N days"
 
## long version ##
date --date="YYYY-MM-DD+N days"
 
## set time zone ##
TZ='TIME_ZONE_HERE' date -d="YYYY-MM-DD+N days"

For example add 20 days to 29/July/2020 as follows:

date -d "2020-07-29+20 days"
 
## Try NYZ time zone ##
TZ='America/New_York' date -d "2020-07-29+20 days"
 
## add 5 days to 14/Aug/2020 for IST time zone
TZ='Asia/Kolkata' date -d "2020-07-29+20 days"
 
eggs="2020/07/29"
days="20 days"
echo "Eggs expiry date $(date -d "${eggs}+${days}")"
How to add days to date and get new date on Linux

Related: Getting Yesterdays or Tomorrows Day With Bash Shell Date Command and How To Format Date For Display or Use In a Shell Script for more information

Subtracting days to date command

Simply try:
date -d "YYYY-MM-DD-N days"
date -d "2020-08-15-5 days"

Monday 10 August 2020 12:00:00 AM IST

Add X days to date and get new date on macOS or BSD

The syntax is slightly different for *BSD family of operating systems including macOS Unix based desktop:

date -v {format}
 
## Display the current date/time ##
date 
 
## Adding 2 days to the current date on macOS / FreeBSD ##
date -v +2d
 
## Subtracting 4 days from the current date on macOS / FreeBSD / OpenBSD / NetBSD etc ##
date -v -4d
 
## Add 20 days to the current date when using bsdutils date using TZ ##
TZ='America/Los_Angeles' date -v +20d

Add a day to date in macOS/*BSD unix

The syntax is as follows:
date -j -v +Nd -f "%Y-%m-%d" "YYYY-mm-dd" +%Y-%m-%d
For example add 20 days to 15/Aug/2020 as follows on macOS or FreeBSD:
date -j -v +20d -f "%Y-%m-%d" "2020-08-15" +%Y-%m-%d
In this final example subtract 10 days from “20/Aug/2020” when using bash shell:
days="10d"
my_date="2020-08-20"
date -j -v -${days} -f "%Y-%m-%d" "${my_date}" +%Y-%m-%d

How to add a day to date in bash on macOS or FreeBSD

Conclusion

You learned how to increment (add) a date in a Bash shell script. Please note that BSD and macOS date command does not support GNU/Linux date command syntax. See date command man page for more information by typing the following man command:
man date

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • RadicalDreamer Aug 18, 2020 @ 12:01

    Pathetic. Can’t you just calculate date in the head? Google it like everyone else. But no, you had to write how to calculate a date for the expiry of dippy eggs.

    • Why? Aug 19, 2020 @ 13:08

      Why negative comments? It is a good howto. Not everyone is experienced shell user.

  • Vonskippy Aug 18, 2020 @ 21:22

    Interested howto article, but really bad real world example. If you can’t figure out what 20 Days past July 29 is without using a computer, you’ve got bigger problems then old eggs.

  • faac Aug 19, 2020 @ 6:21

    There is a typo i your code and the result is way off… Just calculate it in your head:
    7/29 + 20 days should not be 09/07…

    ${eggs} instead of ${egg}

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.