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

Share:
318,245

Related videos on Youtube

conandor
Author by

conandor

Updated on January 20, 2020

Comments

  • conandor
    conandor over 4 years

    How can I print the date which is a day before current time in Bash?

    • conandor
      conandor over 14 years
      Tried date but seems like there is no -d switch in Solaris 10's bash
    • conandor
      conandor almost 14 years
      I found another great solution by installing gnu date (coreutil package) from sunfreeware.
    • frankster
      frankster over 11 years
      nor is there a -d switch in AIX's date
    • Chris F.A. Johnson
      Chris F.A. Johnson over 11 years
      The --date or -d option is only available in GNU date
    • Amir Ali Akbari
      Amir Ali Akbari over 7 years
      See stackoverflow.com/q/15374752/462865 for a DST-safe version of this question.
  • Peter Lindqvist
    Peter Lindqvist over 14 years
    +1 for correction, interesting method, even though it's a bit verbose.
  • martin clayton
    martin clayton over 14 years
    What about if today is the 1st January?
  • conandor
    conandor over 14 years
    I found most are as suggested answer but -d switch is not found in Solaris's date
  • jlliagre
    jlliagre over 12 years
    This method is not 100% reliable (about 98% actually) if you happen to live in a place using daylight saving time.
  • Karthick S
    Karthick S over 12 years
    Hi 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
    zhihong about 11 years
    For 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
    misguided almost 11 years
    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
  • Anton Roslov
    Anton Roslov almost 11 years
    Instead of the 3 lines between the "fi" try "day=echo $(cal $month $year)|tr ' ' '\n'|tail -n 1"
  • kasterma
    kasterma almost 10 years
    On OS X you can install coreutils through brew: see stackoverflow.com/questions/15374752/…
  • bgs
    bgs about 8 years
    date -v-1d on OSX (minus), -v+1d (plus)
  • Noah Spurrier
    Noah Spurrier almost 8 years
    This 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
    kuanb about 7 years
    Erros on macOS Sierra: date: illegal option -- -
  • sumitsu
    sumitsu about 6 years
    You 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: topbug.net/blog/2013/04/14/…
  • phray2002
    phray2002 almost 5 years
    For Solaris, use one time timezone bash $(TZ=America/New_York date +%Y-%m-%d)
  • phray2002
    phray2002 almost 5 years
    Not exactly, you can use bash $(TZ=America/New_York date +%Y-%m-%d)
  • Tõnu Samuel
    Tõnu Samuel over 4 years
    date -d "yesterday 13:00" '+%F' is shorter
  • Andrew Henle
    Andrew Henle over 4 years
    That results in date: illegal option: -d on Solaris. Your answer that relies on non-portable GNU extensions to the POSIX-standard date utility for a question about the non-GNU Solaris platform.