Get the date (a day before current time) in Bash
318,245
Solution 1
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
Solution 2
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
Solution 3
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
Solution 4
date --date='-1 day'
Solution 5
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
Related videos on Youtube
Author by
conandor
Updated on January 20, 2020Comments
-
conandor over 4 years
How can I print the date which is a day before current time in Bash?
-
conandor over 14 yearsTried date but seems like there is no -d switch in Solaris 10's bash
-
conandor almost 14 yearsI found another great solution by installing gnu date (coreutil package) from sunfreeware.
-
frankster over 11 yearsnor is there a -d switch in AIX's date
-
Chris F.A. Johnson over 11 yearsThe
--date
or-d
option is only available in GNU date -
Amir Ali Akbari over 7 yearsSee stackoverflow.com/q/15374752/462865 for a DST-safe version of this question.
-
-
Peter Lindqvist over 14 years+1 for correction, interesting method, even though it's a bit verbose.
-
martin clayton over 14 yearsWhat about if today is the 1st January?
-
conandor over 14 yearsI found most are as suggested answer but -d switch is not found in Solaris's date
-
jlliagre over 12 yearsThis method is not 100% reliable (about 98% actually) if you happen to live in a place using daylight saving time.
-
Karthick S over 12 yearsHi 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.
-
zhihong about 11 yearsFor 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")
-
misguided almost 11 yearsThis doesn't work for the previus day on change of month . For example today is
1 July , 2013
, the result being printed out is2013-6-1347
, whereas the expected result is2013-6-30
-
Anton Roslov almost 11 yearsInstead of the 3 lines between the "fi" try "day=
echo $(cal $month $year)|tr ' ' '\n'|tail -n 1
" -
kasterma almost 10 yearsOn OS X you can install coreutils through brew: see stackoverflow.com/questions/15374752/…
-
bgs about 8 years
date -v-1d
on OSX (minus),-v+1d
(plus) -
Noah Spurrier almost 8 yearsThis 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 }
-
kuanb about 7 yearsErros on macOS Sierra:
date: illegal option -- -
-
sumitsu about 6 yearsYou need to install the GNU version of
date
, available through thecoreutils
package, to make this syntax work on macOS. If you're using Homebrew, you can install it viabrew install coreutils
, after which you should be able to use GNU date through thegdate
command. See: topbug.net/blog/2013/04/14/… -
phray2002 almost 5 yearsFor Solaris, use one time timezone
bash $(TZ=America/New_York date +%Y-%m-%d)
-
phray2002 almost 5 yearsNot exactly, you can use
bash $(TZ=America/New_York date +%Y-%m-%d)
-
Tõnu Samuel over 4 yearsdate -d "yesterday 13:00" '+%F' is shorter
-
Andrew Henle over 4 yearsThat results in
date: illegal option: -d
on Solaris. Your answer that relies on non-portable GNU extensions to the POSIX-standarddate
utility for a question about the non-GNU Solaris platform.