How can I print the date which is a day before current time in Bash?
-
Tried date but seems like there is no -d switch in Solaris 10's bash – conandor Nov 10 '09 at 19:43
-
I found another great solution by installing gnu date (coreutil package) from sunfreeware. – conandor Jul 09 '10 at 06:12
-
nor is there a -d switch in AIX's date – frankster Nov 19 '12 at 15:22
-
See http://stackoverflow.com/q/15374752/462865 for a DST-safe version of this question. – Amir Ali Akbari Oct 19 '16 at 12:35
-
The `--date` or `-d` option is only available in GNU date – Chris F.A. Johnson Nov 27 '12 at 14:27
19 Answers
if you have GNU date and i understood you correctly
$ date +%Y:%m:%d -d "yesterday"
2009:11:09
or
$ date +%Y:%m:%d -d "1 day ago"
2009:11:09

- 327,991
- 56
- 259
- 343
-
1I found most are as suggested answer but -d switch is not found in Solaris's date – conandor Nov 11 '09 at 04:30
-
3For the usage on Ubuntu with date function: $(date +%Y:%m:%d -d "1 day ago"), output is: 2013:04:21. And if you want the date 10 days before, you can use $(date +%Y:%m:%d -d "10 days ago") or $(date +%Y:%m:%d -d "10 day ago") – zhihong Apr 22 '13 at 16:08
-
On OS X you can install coreutils through brew: see http://stackoverflow.com/questions/15374752/get-yesterdays-date-in-bash-on-linux-dst-safe#comment35873979_22374082 – kasterma Jun 04 '14 at 08:30
-
18
-
3This bash function will work on both Linux and OSX. Basically it tries the GNU Linux style first. If that fails it tries the OSX style. Call it with an argument of the number of days in the past you want the date. If you pass no argument it assume 0 days. This code is a one-liner, so cut and paste should work -- no line endings assumed. `date_days_past () { days_past=${1:-0}; if ! date -v-${days_past}d +%Y%m%d 2>/dev/null; then date --date="-${days_past} day" +%Y%m%d; fi }` – Noah Spurrier May 16 '16 at 21:29
-
For Solaris, use one time timezone ```bash $(TZ=America/New_York date +%Y-%m-%d) ``` – phray2002 Jul 24 '19 at 04:27
-
If you have BSD (OSX) date
you can do it like this:
date -j -v-1d
Wed Dec 14 15:34:14 CET 2011
Or if you want to do date calculations on an arbitrary date:
date -j -v-1d -f "%Y-%m-%d" "2011-09-01" "+%Y-%m-%d"
2011-08-31

- 681
- 5
- 3
date --date='-1 day'

- 3,843
- 23
- 29
-
4
-
3You need to install the GNU version of `date`, available through the `coreutils` package, to make this syntax work on macOS. If you're using Homebrew, you can install it via `brew install coreutils`, after which you should be able to use GNU date through the `gdate` command. See: https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/ – sumitsu Feb 16 '18 at 19:56
MAC OSX
For yesterday's date:
date -v-1d +%F
where 1d defines current day minus 1 day. Similarly,
date -v-1w +%F - for previous week date
date -v-1m +%F - for previous month date
IF YOU HAVE GNU DATE,
date --date="1 day ago"
More info: https://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html

- 708
- 9
- 19
Well this is a late answer,but this seems to work!!
YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`;
echo $YESTERDAY;

- 129
- 4
- 11
Sorry not mentioning I on Solaris system. As such, the -date switch is not available on Solaris bash.
I find out I can get the previous date with little trick on timezone.
DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE

- 3,637
- 6
- 29
- 36
-
1This method is not 100% reliable (about 98% actually) if you happen to live in a place using daylight saving time. – jlliagre Nov 18 '11 at 07:31
-
Not exactly, you can use ```bash $(TZ=America/New_York date +%Y-%m-%d) ``` – phray2002 Jul 24 '19 at 04:28
-
See https://stackoverflow.com/a/21283578/2089784 for a DST-safe version – madprogramer Oct 02 '22 at 10:39
date +%Y:%m:%d -d "yesterday"
For details about the date format see the man page for date
date --date='-1 day'

- 10,122
- 3
- 41
- 60
date -d "yesterday" '+%Y-%m-%d'
or
date=$(date -d "yesterday" '+%Y-%m-%d')
echo $date

- 1,138
- 14
- 12
-
1That results in `date: illegal option: -d` on Solaris. Your answer that relies on non-portable GNU extensions to [the POSIX-standard `date` utility](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html) for a question about the non-GNU Solaris platform. – Andrew Henle Jan 19 '20 at 16:57
Use Perl instead perhaps?
perl -e 'print scalar localtime( time - 86400 ) . "\n";'
Or, use nawk and (ab)use /usr/bin/adb:
nawk 'BEGIN{printf "0t%d=Y\n", srand()-86400}' | adb
Came across this too ... insane!
/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);printf "0t%d=Y\n", $2-86400}' | adb

- 76,436
- 32
- 213
- 198
Not very sexy but might do the job:
perl -e 'my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);$year += 1900; $mon+= 1; printf ("YESTERDAY: %04d%02d%02d \n", $year, $mon, $mday)'
Formated from "martin clayton" answer.

- 306
- 4
- 12
You could do a simple calculation, pimped with an regex, if the chosen date format is 'YYYYMM':
echo $(($(date +"%Y%m") - 1)) | sed -e 's/99$/12/'
In January of 2020 it will return 201912 ;-) But, it's only a workaround, when date does not have calculation options and other dateinterpreter options (e.g. using perl) not available ;-)

- 524
- 4
- 10
short answer (GNU format):
date +%Y-%m-%d -d "-2 day"
if you are using OSX, but you need create for GNU compatible, install coreutils first
brew install coreutils
then edit your profile with:
#gnu coreutils first
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
re-start your terminal, and now you able to use GNU format!

- 1,729
- 9
- 15
#!/bin/bash
OFFSET=1;
eval `date "+day=%d; month=%m; year=%Y"`
# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=`expr $day - $OFFSET`
if [ $day -le 0 ] ;then
month=`expr $month - 1`
if [ $month -eq 0 ] ;then
year=`expr $year - 1`
month=12
fi
set `cal $month $year`
xday=${$#}
day=`expr $xday + $day`
fi
echo $year-$month-$day

- 1,189
- 2
- 16
- 36
-
+1 for correction, interesting method, even though it's a bit verbose. – Peter Lindqvist Nov 10 '09 at 10:31
-
1Hi medoix, I am not able to find the meaning of ${$#}. I know that $# stands for the number of arguments to a script/function. But I am not able to relate to this. Please help. – Karthick S Feb 01 '12 at 08:25
-
This doesn't work for the previus day on change of month . For example today is `1 July , 2013` , the result being printed out is `2013-6-1347`, whereas the expected result is `2013-6-30` – misguided Jun 30 '13 at 22:57
-
Instead of the 3 lines between the "fi" try "day=`echo $(cal $month $year)|tr ' ' '\n'|tail -n 1`" – Anton Roslov Jul 10 '13 at 10:18
yesterday=`date -d "-1 day" %F`
Puts yesterday's date in YYYY-MM-DD format into variable $yesterday
.

- 167,459
- 57
- 363
- 519
DST aware solution:
Manipulating the Timezone is possible for changing the clock some hours. Due to the daylight saving time, 24 hours ago can be today or the day before yesterday.
You are sure that yesterday is 20 or 30 hours ago. Which one? Well, the most recent one that is not today.
echo -e "$(TZ=GMT+30 date +%Y-%m-%d)\n$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1
The -e parameter used in the echo command is needed with bash, but will not work with ksh. In ksh you can use the same command without the -e flag.
When your script will be used in different environments, you can start the script with #!/bin/ksh or #!/bin/bash. You could also replace the \n by a newline:
echo "$(TZ=GMT+30 date +%Y-%m-%d)
$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1

- 19,067
- 2
- 23
- 43
Try the below code , which takes care of the DST part as well.
if [ $(date +%w) -eq $(date -u +%w) ]; then
tz=$(( 10#$gmthour - 10#$localhour ))
else
tz=$(( 24 - 10#$gmthour + 10#$localhour ))
fi
echo $tz
myTime=`TZ=GMT+$tz date +'%Y%m%d'`
Courtsey Ansgar Wiechers
For my case I needed a portable solution to get the number of seconds since the epoch one hour ago, and this worked on both ubuntu and macOS:
$(( $(date +%s) - 3600 ))
Explaining this:
$(( ))
is doing an arithmetic expansiondate +%s
seems to be a portable way to get seconds since epoch- 3600 seconds in an hour
Hope this helps!

- 116
- 1
- 4