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 shell script? 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 or BSD/date command, which is designed to handle relative date calculation such as:

  • 1 Year
  • 2 Days
  • 2 Days ago
  • 5 Years
  • yesterday
  • tomorrow
  • next week

Let us see how to get yesterday’s date in bash on Linux operating systems.

Getting Yesterday’s or Tomorrow’s Day with bash on Linux or Unix

The syntax and sample examples are as follows:

GNU date syntax to get yesterday’s date in bash

The syntax is as follows:

date  --date="STRING"
date  --date="next Friday"
date  --date="2 days ago"
date  --date="yesterday"
date  --date="yesterday" +"%format"
# Get yesterday's date in dd-mm-yy format
date  --date="yesterday" +"%d-%m-%y"
date  --date="yesterday" +"%m-%d-%y" # US date format
date  --date="yesterday" +"%Y-%m-%d" # YYYY-mm-dd format
## store y'day date in a shell variable called yday and display it ##
yday=$(date  --date="yesterday" +"%Y-%m-%d")
echo "$yday"

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. See “How To Format Date For Display or Use In a Shell Script” for more about +"%format" stings.

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)

GNU date command 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 in bash script on Linux

Type the following command
$ date --date="-1 days ago"
$ 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

Linux Yesterdays or Tomorrows Day With Bash Shell Date Command

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'

Show the date in the future ($now + 6 months + 15 days), enter:
$ date --date='6 months 15 day'
Display 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

Let us 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'
# date --set='1 day ago'
# 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:
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)
date -d "$t_FIRST_DAY_NEXT_MON -1 day" +%b-%d-%Y

Tomorrows day with bash

A note about BSD date on FreeBSD or macOS

The syntax is as follows to get yesterday’s date in bash on macOS or FreeBSD:

date -v VAL # yesterday 
date -v -1d # yesterday 
date -v +1d # tomorrow 
date -v -1w # 1 week ago
date -v +1w # next week 
## formatting date ##
date -v -2w +"%F" # 2 weeks ago in YYYY-mm-dd format
date -v +1d +"%d-%m-%y" # dd-mm-yy format
date -v +1d +"%m-%d-%Y" # mm-dd-yyyy (US) format

Get yesterday's date in bash on Linux and FreeBSD or macOS UNix
If VAL is numeric, one of either y, m, w, d, H, M or S must be used to specify which part of the date is to be adjuste. See man page on macOS Unix or FreeBSD system:
$ man date


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:

  1. man date
  2. How To Format Date For Display or Use In a Shell Script
  3. GNU/date documentation page

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 75 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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
75 comments… add one
  • kiuze Jul 4, 2007 @ 21:19

    Thank you for this info!

    There seems to be a small typo on first example:

    date -"date=1 days ago"

    should be

    date --"date=1 days ago"

    with two “-“s

    • Rajesh Sep 23, 2010 @ 15:42

      Is there any way to caculate the current staturday’s date based on the input date passed not from the system date?

      Please let me know

  • 🐧 nixCraft Jul 4, 2007 @ 21:55


    Thanks for the heads up!

  • Hebi-kai Jul 6, 2007 @ 17:48

    How do you use the –date= string to start at one point and count ahead? I want to start at 20060108 and display the date of each sunday until July 1, 2007. If I say “–date=’week'” it gives me one week from today. How can I add ‘week’ to a date, such as 20060108?

  • Pablo Armando Sep 18, 2007 @ 18:34

    You can use someting like this to:

    $date -d “yesterday” +%d
    $date -d “yesterday” +%m


    $date -d “yesterday” +%m%d%y

    • Dharmendra Devireddy May 8, 2012 @ 12:13

      Thank you..it is working 🙂

  • BK Oct 25, 2007 @ 0:01

    Given a date, how do I relatively find a day of the week? Example: For the date 20070807, how do I find out the date of the Monday of that week?

  • sai Oct 30, 2007 @ 13:22

    how to find the dates between 2 dates.
    i need a command to get the list of dates between 2 dates.

  • Sri Nov 30, 2007 @ 6:56

    I want previous date and Next date.
    I tried the above commands,byt its giving me the following usage.
    bash-3.00$ date –date=”yesterday”
    date: illegal option — –
    Usage: date [-u] [+format]
    date [-u] [mmddhhmm[[cc]yy]]
    date [-a [-]sss.fff]

    I am using HP-UX machine

    • Geneyo Jun 2, 2011 @ 19:27

      I am having similar error on HP. How did you resovle your problem?

      • Dave Feb 1, 2012 @ 15:10

        I do not think HP-UX uses the GNU tools, such as /bin/date.
        You need more that just bash, presuming you typed “date –date=….”
        In other words, “date {dash{dash} date=”
        There are many little details like that which make HP-UX annoying.
        I am spoiled by Linux.

      • Rd Sep 24, 2013 @ 12:59

        It may not work for HP-UX. It is for Linux.

  • Deiva Jun 11, 2008 @ 9:31

    Excellent! Very useful while writing scripts.


  • Omri A Dec 23, 2008 @ 9:45

    Or you can use the following to get tomorrow’s date:

    x="`date '+%m'`"
    y="`date '+%Y'`"
    z="`date '+%d'`"
    z="`expr ${z} + 1`"
    cal ${x} ${y} | grep -w ${z} > /dev/null || {
            x="`expr ${x} + 1`"
            [ "${x}" -lt "10" ] && x="0${x}"
            [ "${x}" = "13" ] &+ 1`"
    [ "${1}" = "MMDDYYYY" -o "${1}" = "mmddyyyy" -o -z "${1}" ] &${z}${y}"
            echo ${tomorrow}
            exit 0
    [ "${1}" = "DDMMYYYY" -o "${1}" = "ddmmyyyy" ] &${x}${y}"
    } || {
            echo "Invalid format! Using the default:"
    echo ${tomorrow}
  • Beniori Feb 10, 2009 @ 3:15

    It is very useful. However, I would like to know how can I type the end of month.

  • Andrew McGlashan Mar 15, 2009 @ 16:46

    I’ve been wanting to work out last day of a month too. I just worked it out:

    date -d ‘next month – ‘`date +%d`’day’


    • Philippe Petrinko Sep 17, 2010 @ 16:03

      @Andrew & @Sourav

      May be wordpress word entry has eaten some of your characters,
      here is an equivalent rephrase of what you gave,
      to print last day of current month on GNU/BASH shell

      date --date "next month - $(date +%d) day"

  • Sourav Mar 16, 2009 @ 11:20

    I am not sure, but when I am trying to use the above command:
    date –date=”1 days ago”
    I get the following error:
    date: illegal option — –
    date: illegal option — d
    date: invalid argument — te=1 days ago
    usage: date [-u] mmddHHMM[[cc]yy][.SS]
    date [-u] [+format]
    date -a [-]sss[.fff]
    Just FYI: The flavour Unix, I am using is SunOS, and my shell is KORN Shell.

    • kiran Oct 21, 2016 @ 10:10

      Hi Sourav

      This is kiran you just type like below

      date --date='1 day ago'

      you typed like date --date=' 1 days ago'

      so that you getting error i think.

  • Jason Tepoorten Mar 19, 2009 @ 0:27

    Hi Sourav.

    I don’t think the Solaris “date” command is as extensive as the LINUX date command.

    I this informaiton from Solaris 10’s date command when using “date –date”:
    $ date –date
    date: illegal option — date
    usage: date [-u] mmddHHMM[[cc]yy][.SS]
    date [-u] [+format]
    date -a [-]sss[.fff]

    I found an article on getting yesterday’s date on Solaris from the shell. The article provides a PERL shell command:
    perl -e ‘print scalar(localtime(time – 86400)), “n”‘

    The URL for that article is http://sysunconfig.net/unixtips/prevdate.txt

  • Jason Tepoorten Mar 19, 2009 @ 1:10

    Hi Sourav.

    I’ve done some digging for determining yesterday’s and tomorrow’s date and I found a Korn Shell script posted at the URL http://www.computing.net/answers/unix/getting-old-date-into-variable/5223.html. Look for Response 10 for the source of the KSH script.

    This KSH script runs under KSH on Solaris 10.

  • Sourav Mar 24, 2009 @ 10:49

    Hi Jason,

    Firstly thanks a ton to find out the source of my problem.

    But just FYI, i tried to give the below commands, and it helped me to get yesterday’s and tomorrow’s date. Hope this helps everyone who faces the same issue as me.

    $ TZ=$TZ+24 date “+%D”……….for yesterday’s date
    $ TZ=$TZ-24 date “+%D”………..for tomorrow’s date

    Thanks again…

    • Jason TEPOORTEN May 30, 2010 @ 0:50

      Hi Sourav.
      I apologise for the late reply. I never got a posting notification :(.
      I’ll try this command on my client’s UltraSPARC IV SunOS 10.0 KSH environment.
      How did you find this one?
      Kind Regards,
      Jason TEPOORTEN

  • Ibrahim Jul 12, 2009 @ 14:55

    The “date –date” option only works with GNU version of date. It will most probably not work on AIX, Solaris or HP-UX. If you want to calculate yesterday’s date, you can also use this shell script.

    Slight modification may be necessary to calculate tomorrow’s date.

  • 'Tosin Sep 25, 2009 @ 15:28

    Thanks so much, this has been so helpful.

  • Aleks Nov 18, 2009 @ 17:15

    @Andrew McGlashan
    thx a whole bunch!

  • Ibrahim Khaleel Dec 2, 2009 @ 14:47

    I wanted output as this format – 2009_12_01, the below worked for yesterday
    echo `date --date="yesterday" '+20%y_%m_%d'`

  • Kassim May 18, 2010 @ 7:18

    How to find the commands which are executed yesterday?

    • Arthur Jul 14, 2010 @ 7:22


      Use the command

      $ history

  • joi sagum Sep 13, 2010 @ 2:42

    How this this done in AIX?

  • Reynold P J Nov 6, 2010 @ 23:34

    I usually find really helpful articles in this site….
    Keep up the good work:)

  • bob Mar 1, 2011 @ 0:11

    doesn’t work on Mac OS X

    • Tom Oct 1, 2013 @ 23:29

      for mac use -v-Xd (X days ago) example:

      date -v-1d “+%Y-%m-%d”

  • Skyblaster Mar 24, 2011 @ 15:14

    Hello guys,

    Here is a nice one it calculate the latest working day of the month maybe it’s usefull

    TODAY=`date +%d-%m-%Y`
    if [ ${TODAYR} -eq 5 ]
            DD2=`TZ="GMT-72" date +'%d'`
            DD2=`TZ="GMT-24" date +'%d'`
    if [ ${DD} -gt ${DD2} ]
     another script on the latest work day of the month
  • Skyblaster Mar 24, 2011 @ 15:16

    Hello guys,

    (i missed the TODAYR (is day number from 1-7) in my prev email)

    TODAY=`date +%d-%m-%Y`
    TODAYR=`date +%u`
    if [ ${TODAYR} -eq 5 ]
            DD2=`TZ="GMT-72" date +'%d'`
            DD2=`TZ="GMT-24" date +'%d'`
    if [ ${DD} -gt ${DD2} ]
  • seldi Jun 4, 2011 @ 8:34

    It was very helpfully for me.

  • Nico Jun 8, 2011 @ 13:05

    You have a little flaw in there:

    date --date "2 months 5 days ago"

    doesn’t return the date two months and five days ago but rather the date which was five days before the date which will be in two months. It tells date something like “plus 2 months minus 5 days”.

    Of course, this might be what you wanted. But to be corresponding to your descripten, it would have to be

    date --date "2 months ago 5 days ago"
    • Philippe Petrinko Jun 14, 2011 @ 15:02

      Yes Rigth Nico.

      Vivek should amend this one, right Vivek ?

      • 🐧 nixCraft Jun 15, 2011 @ 0:35

        @Nico / Philippe,

        This article has been updated with information on the date command with additional examples.

    • victor Jan 9, 2012 @ 20:03

      It does work but it should look like:
      date --date "-2 months 5 days ago"

      • Philippe Petrinko Jan 9, 2012 @ 23:35

        Hi Victor.

        I am afraid syntax is unclear to you.
        Regarding each token (day, month) syntax may be either using minus sign, either using “ago” word. So that all 4 expressions are referring to one unique date:

        date --date "2 months ago 5 days ago"
        date --date "- 2 months - 5 days"
        date --date "2 months ago - 5 days"
        date --date "- 2 months 5 days ago"

        Is it clear to you now?

        — Philippe

  • jay Jun 13, 2011 @ 21:57

    hello –

    I am looking for some ideas to print out dates in between 2 dates in YYYY-mm-dd format in unix.

    any advice is appreciated.


  • jay Jun 13, 2011 @ 22:26
    for ((i="2010-12-01"; i<="2011-06-14"; $currentDateTS+=86400))
       echo $i"n"

    i know something is not right here please advice.

  • rani Jun 14, 2011 @ 11:57

    we are using this command in our pull script to get day-1 data.
    vYdy_yyyy_mm_dd=`date --d='1 days ago' +"%Y-%m-%d"`
    But i want data from 2010-08-1 to 2010-05-19 at a time.Could you please help me out in changing the command.
    vYdy_yyyy_mm_dd=`date --d='1 days ago' +"%Y-%m-%d"`

  • Chunky Oct 10, 2011 @ 14:28

    Hi all,

    I am having a script to check filesize’s difference between two consecutive days dumps. The DB dumps are created every WORKING day (Monday-Friday) and they all have the same name + the full name day of the week, i.e. dumpFile.Monday.dump, dumpFile.Tuesday.dump … dumpFile.Friday.dump.

    In my script I try to use `date +%A` for today and `date –date=yesterday +%A` for yesterday, however this (obviously) doesn’t work for Monday, when I get yesterday as being Sunday, whereas I want Friday.

    I hacked a bit the script with an if loop, if today = Monday, then use yesterday = Friday, else use `date --date=yesterday +%A`, but is not quite an elegant solution.

    I wonder if there’s a way that on any given date, to be able to get previous WORKING day.


    • Philippe Petrinko Jan 10, 2012 @ 10:18

      Hi Chunky,
      You post is old, but here is an answer.

      Any system can only elaborate data based on what you feed it with, data & algorithm.
      A “working day” is a humain professional concern, and, AFAICS, this information is not stored _yet_ in any standard *nix system.

      hacked a bit the script with an if loop, if today = Monday, then use yesterday = Friday, else use `date –date=yesterday +%A`, but is not quite an elegant solution.

      Elegant or not, system has to store this information before giving it back to you.

      FTTOMH, as you want to compare the 2 most recent dump files, another way to do it without this worrying about working days would simply be to select the 2 most recent dump files to compare them, based on modification time.

      Nevertheless, on one hand, this involves at least extra coding instead of a simple test, and may be less secure, for instance if you do not check that every dump went fine.

      On the other hand, this algorithm would work in any situation, that is, it would always test the 2 most recent dump files, whatever calendar events, closing days, holidays, whatsoever.

      What do you think Chunky?

      — Philippe

  • Divya Mar 2, 2012 @ 6:31

    Hey I make a query..please let me know if this is possible…
    StartDate=`date --date='1 day ago' +"%y%m%d"`

    i am using above command to use yesterday’s date. In “1 day ago” i want to make 1 as variable like $i to change it when needed. as 1,2,3 etc…

  • hroptatyr Apr 20, 2012 @ 9:59

    Hey Chunky,
    here’s another take on your problem if you can afford to have an extra binary that does the job. Look at my dateutils project (http://hroptatyr.github.com/dateutils). It can deal with business days natively, use the b suffix:

    dadd 2012-04-16 -1b
    => 2012-04-13

  • qcjn Oct 28, 2012 @ 5:33

    date --date "3 month - $(date +%d) day + 1 day"
    tried it using Philippe Petrinko and adding one day, this show the first day of the mont in 3 month. Just to show how much you can do.
    date -d '-3 days -2 hours -10 minutes' '+%d/%m/%Y %H:%M' # this second part changes the format.
    Thanks for this tutorial

  • Melanie Nov 5, 2012 @ 16:53

    I found that when the clocks are set back an hour in November, here in the US, I could not use 1 day ago in the date command and have to use 26 hour ago. If you don’t do that you could accidentally work with the wrong file.

  • Martin Mar 1, 2013 @ 18:38

    Just beautiful…

  • kokosny Jul 31, 2013 @ 23:18

    for bsd users, to get yesterdays date or any past or future date.
    You can customize the format too.

    yest=$(date -v-1d +"%m_%d_%Y" )

    hope it helps

  • Kumar Mar 17, 2014 @ 8:05

    Im getting following error while copying file from one server to other , even tried by defining and passing a variable same error.

    spawn scp  /var/backup/full_bkup-$(date --date "yesterday" +%Y%m%d).sql.tgz "root@IPAddress:/home/ravi
    "can't read "(date --date "yesterday" +%Y%m%d)": no such variable
        while executing
    "spawn scp  /var/backup/full_bkup-$(date --date "yesterday" +%Y%m%d).sql.tgz "root@IPAddress:/home/ravi""
        (file "./script3.sh" line 5)


  • Dipun Apr 8, 2014 @ 16:25

    I am just learning Unix script and i started to write shell script.
    I want to see 7days back date from current date format should be mm/dd/yyyy.
    Could you please help me.

    Dipun Rath

    • Nick Apr 27, 2014 @ 18:21

      Hi Dipun,
      Welcome to shell scripting! Using bash and sh, I believe this should get you what you’re looking for.

      date -v -7d +"%m/%d/%Y"

      Hope this helps

  • Abhilash Kumar Apr 24, 2014 @ 12:31

    date -v -1d

    In order to get 1 day back date using date command:
    date -v -1d
    It will give (current date -1) means 1 day before .
    date -v +1d
    This will give (current date +1) means 1 day after.

    Similarly below written code can be used in place of “d” to find out year,month etc

  • Toh Chai Hou Jun 5, 2014 @ 3:40

    Hi Guys,

    I am new to this. Saw the email threads on having the dates. I am running Win7 platform. Would want to carry out a CCTV backup for previous day folder in YYYYMMDD folder. Can advise on the comandline please.

  • El Nico Jun 5, 2014 @ 8:16

    @Toh Chai Hou:
    This is very linuxy here, so I guess you won’t get help about the Win7 platform. But you might start with this

  • Andrew Kendall Jun 13, 2014 @ 9:38

    I’ve got a requirement/variation not covered above (I think):
    Using linux bash, how would I calculate the year for last month if this month is January?
    Obviously for the Months Feb-Dec, the year for last month is the same as this month.
    Would this work please?
    date --date='1 month ago' +%Y
    As it’s currently May it’s giving me the result I should get but I don’t have a system to play with where I can change the system date to confirm it works for January.
    Thanks in advance,

    • Philippe Petrinko Jun 25, 2014 @ 14:11



      date --date="1 month ago" +%Y

      would give you appropriate year for any month.

      Used in january, you would definitely get previous year.

      — Philippe

      • Andrew Kendall Jun 26, 2014 @ 7:38

        Thanks Pilippe. I’d given up on getting an answer.
        Best regards

        • Philippe Petrinko Jun 26, 2014 @ 15:38

          My pleasure, @Andy

          By the way, remember that you should test any code in a test environment, not on a production server. 😉


  • Craig Aug 11, 2014 @ 6:14


    I’m trying to find the date of Monday before a date.

    That is on the 05-Feb-2014, the Monday date was 03-Feb-2014.
    I have tried to:
    # date --date "last monday - $(date -d '2014-02-05' +%d) day"
    Wednesday 30 July 00:00:00 EST 2014

    # date --date "$(date -d '2014-02-05' +%Y%m%d) last monday"
    Wednesday 5 February 00:00:00 EST 2014

    But I want “Monday 3 February 00:00:00 EST 2014” which I can convert to 2014-02-03 (YYYY-MM-DD).

    Thank you in advance.


    • Philippe Petrinko Aug 20, 2014 @ 6:57


      For instance, I would proceed this way, using your specific date as a parameter

      1) determine which day of week is your parameter date (the date that your search for its previous monday)
      2) use (1) this to get back using the “days ago” expression date

      But take care! If parameter date is a monday, you have to get back 7 days ago.

      Show us what you could do this way! 🙂

      — Philippe Petrinko

      • Philippe Petrinko Aug 20, 2014 @ 10:51

        Well, both ways works, subtract or use “ago”. I’ve got monday both ways.

        So what did you come to? Show us your code!

  • Philippe Petrinko Aug 20, 2014 @ 6:59

    sorry I meant:

    2) use (1) this to get back using _substraction_ expression date

  • Craig Aug 21, 2014 @ 5:54

    This probably could be a lot cleaner.

    #-- dateconv.sh
    checkday=`date --date=$(echo ${indate}) +%u`
    case ${checkday} in
       1) echo "Monday - Hooray -- `date --date=$(echo ${indate} "+%c")`"
       *) indate=`expr ${indate} - ${checkday} + 1`
          checkday=`date --date=$(echo ${indate}) +%u`
          case ${checkday} in
             1) echo "Monday - 2nd Time -- `date --date=$(echo ${indate} "+%c")`"
             * ) echo "Failed - ${checkday} - Should not happen."
    echo "End Script"

    > dateconv.sh 20140205
    Monday – 2nd Time — Mon 03 Feb 2014 00:00:00 EST
    End Script

    Thanks Phillippe for your help.

  • R Mistry Dec 22, 2014 @ 6:02

    Thank you for the tutorial. I am using this to remove the database dump taken yesterday, immediately after creation of latest dump. The shell used in this as below –

    sysdate=`date +%Y%m%d%H%M`
    mysqldump nwsm> /usr1/database_backups/nwsm_bckup/nwsm_$sysdate
    prbckup=$(date --date="-1 day" +"%Y%m%d"2350)
    rm /usr1/database_backups/nwsm_bckup/nwsm_$prbckup

    Thanks again. It helped a lot. 🙂

  • gsavix Mar 15, 2015 @ 22:38

    thanks for your clean example of date use for linux. i get your samples
    axsomedate=$(date --date="1 day ago" +"%Y-%m-%d") more elegant of what i used before using:
    axsomedateold=`date +'%Y-%m-%d' -d "1 day ago"`;
    thanks for your time posting good web examples!

  • raj Feb 25, 2016 @ 0:12

    Thank you Sir.

  • Drew Mar 4, 2016 @ 18:12

    How do you get the last DOW of a month? or like the third DOW of a month?
    date -d 'last sunday' gives you the date of the previous sunday, not the last sunday of the calendar month.

    date -d'second sunday' gives you the next sunday relative to the current date.
    So today, Friday March 4th, you get the following output:
    -bash-4.1$ date -d 'first sunday'
    Sun Mar 6 00:00:00 EST 2016
    -bash-4.1$ date -d 'second sunday'
    Sun Mar 6 00:00:01 EST 2016
    -bash-4.1$ date -d 'third sunday'
    Sun Mar 20 00:00:00 EDT 2016

  • mary Mar 24, 2016 @ 6:52

    how to get dates of all ‘monday’s in march 2016 in mm/dd/yyy format using unix

  • Jam Apr 23, 2016 @ 1:01

    How can you use date to find a holiday, let’s say “mother’s day” (second sunday of May)?

  • Omer Oct 21, 2016 @ 9:55

    This is what helped me in getting yesterdays date in an SFTP file transfer.

    date -d "yesterday 13:00 " '+%Y%m%d'

  • Sandra Anderson Apr 25, 2021 @ 18:01

    Wow, I did not know I can do this from the shell just using `date`.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum