[yesterday date in shell/unix]

Solved
Bonjour,

Which command (in shell-UNIX) displays yesterday's date?
PS: I clarify that it is for a ksh script

--
Thank you for your help

12 answers

  1. Moderator
    Ben if it works, the proof is:
    In bash [jp@Mandrake jp]$ date --date '1 days ago' Tue Aug 2 11:38:36 CEST 2005 We switch shell (the command is an alias) [jp@Mandrake jp]$ kosh $ date --date '2 days ago' Mon Aug 1 11:39:00 CEST 2005 $ date --date '1 days ago' Tue Aug 2 11:39:22 CEST 2005 
    ;-))
    --
    Z'@+...che.
    JP : Zen, my Nuggets ! ;-) Knowledge is only good if it's shared. 
    15
  2. the syntax date X days ago does not exist on all Linux systems, not at all on proprietary Unix systems and not on all BSD systems either!

    to be banned forever as a consequence.
    one of the simple solutions:
    expr `date +%d` - 1


    and of course if we want to manage the month and the year we need to create a shell script
    that checks if the day is 0 then run cal of the previous month to see if it ends with 28, 29, 30, 31 and if it’s January subtract 1 from the year

    nothing simpler really
    0
    1. Great! You're saving our lives!
      0
  3. if [ `expr `date +%d` - 1` -le 0 ]; then cal | grep -E "28|29|30|31" | awk ........... fi 


    anyway, I'm going to write it anyway...
    1
    1. Here is a script that should work:

      #!/bin/ksh
      #
      set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
      set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
      #
      # works on Linux
      #
      # date -d '1 days ago'
      #
      YESTERDAY=$((`date +%d` -1))
      MONTH=`date +%m`
      YEAR=`date +%Y`
      NDAY=`date +%u`
      WEEKDAY=${DAYS[`date +%u`]}
      #
      if [ $YESTERDAY -eq "0" ];
      then
      #
      MONTH=$((MONTH-1))
      #
      if [ $MONTH -eq "0" ];
      then
      #
      MONTH=12
      YEAR=$((YEAR-1))
      #
      fi
      #
      set `cal $MONTH ${YEAR}`
      shift $(($# - 1))
      YESTERDAY=$1
      #
      fi
      #
      TMONTH=${MONTHS[MONTH]}
      YEAR2=${YEAR##20}
      #
      # uncomment next line for debugging
      #
      echo ${WEEKDAY} ${YESTERDAY} ${TMONTH} ${YEAR}
      #
      echo ${YESTERDAY}${MONTH}${YEAR2}
      #
      5
      1. Thank you slamagirand, it's perfect!!!
        0
    2. Contributor
      Hello,

      You can also convert the current date to seconds since Epoch, then subtract 86400 (24*60*60) from the result and reconvert the obtained number to the desired date format.

      See man date, and man strftime for the formats.

      Dal
      1
      1. Under Solaris and HP-UX, there is a simpler solution than creating a script.
        Just play with the time zone (system variable $TZ).

        Example:
        # echo `TZ=MET+24 date +"%D"`
        gives: 11/28/05 (today is 11/29/05)

        The “+24” corresponds to the number of hours to "subtract" (+) from the current time; if you want to add them, use - (see below)
        %D corresponds to the date formatting (see man date): mm/dd/yy

        Similarly, you could get the date for tomorrow in the format yyyymmdd, for example….
        #echo `TZ=MET-24 date +"%Y%m%d"`
        gives: 20051128

        ... or the day after tomorrow (only works under Solaris):
        #echo `TZ=MET-48 date +"%Y%m%d"`
        this last command doesn’t work under HP-UX because it seems you can't go around the earth more than once with HP :p
        14
        1. On Solaris, it works for a maximum of a few days' difference.
          For more than that, you need to compile a file with "zic", e.g.:

          -bash-3.00$ cat Delta
          # Example: 100 days in the future 100*24 hours
          Zone POSE/Zulu+2400 2400 - POSE
          # Example: 365 days in the past 365*24 hours
          Zone POSE/Zulu-8760 -8760 - POSE
          # Example: 1 year and 3 months in the past: (365+90)*24 hours
          Zone POSE/Zulu-10920 -10920 - POSE
          # Example: 2 years and 3 months in the past: (2*365+90)*24 hours
          Zone POSE/Zulu-19680 -19680 - POSE
          # Example: 5 years and 3 months in the past: (5*365+90)*24 hours
          Zone POSE/Zulu-46008 -46008 - POSE

          -bash-3.00$ /usr/sbin/zic Delta
          -bash-3.00$ TZ=POSE/Zulu+2400 date
          Thu Jan 19 12:36:59 POSE 2012
          -bash-3.00$ TZ=POSE/Zulu-46008
          -bash-3.00$ date
          Wed Jul 12 12:32:20 POSE 2006
          0
        2. Great, it works thank you so much =)
          0
      2. Here is the solution to your problem. It works and it's ksh AIX...

        GetDate()
        { # GetDate nDays [format]
        # Example of usage: export NAMEDIR=$(GetDate -1 '+%Y.%m.%d')

        typeset -i nDays=$1; format=$2
        eval $(echo $TZ | sed '
        s!\([^-0-9]*\)\([-0-9]*\)\(.*\)!typeset -i localOffset=\2;zon1=\1;zon2=\3!')
        TZ=$zon1$((localOffset-24*nDays))$zon2 date $format

        }


        To be used in KSH function without moderation...
        Copy/Paste everything that is in bold...
        1
        1. Thank you for this code, it really helped me out!! (and thanks Google)

          My problem was slightly different, I wanted to find the date of the last Sunday...

          Thanks to your script I was able to do this:

          case $(date +%a) in
          Mon ) diff=-1;;
          Tue ) diff=-2;;
          Wed ) diff=-3;;
          Thu ) diff=-4;;
          Fri ) diff=-5;;
          Sat ) diff=-6;;
          Sun ) diff=0;;
          esac
          DATE1=$(GetDate $diff '+%d/%m/%Y')

          It works but it's not very pretty :)

          Would it be possible to make a function GetDate2? with $1 = the day to get (for last Sunday we put Sun) and $2 still the format

          I'm sure it could be done recursively too :)
          0
      3. To calculate the date for J-1, don't overthink it:
        DATE=$((`/bin/date +'%Y%m%d'` - 1))
        There you go, it's in YYYYMMDD format, then, do it your way!
        @+
        0
        1. It doesn't work on the 1st of the month!!
          0
      4. ```bash #!/bin/ksh
        #
        set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
        set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
        set -A MONTH_ 12 01 02 03 04 05 06 07 08 09 10 11 12
        set -A DAYS_ 09 01 02 03 04 05 06 07 08 09
        #
        # works on Linux
        #
        # date -d '1 days ago'
        #
        #YESTERDAY=$((`date +%d` -1))
        date1=20070101
        YESTERDAY=$((`date -d $date1 +%d` -1))
        MONTH=`date -d $date1 '+%m' `
        YEAR=`date -d $date1 '+%Y' `
        NDAY=`date -d $date1 '+%d' `

        WEEKDAY=${DAYS[`date +%u`]}
        #
        if [ $YESTERDAY -eq "0" ];
        then
        #
        MONTH=$((MONTH-1))
        #
        if [ $MONTH -eq "0" ];
        then
        #
        MONTH=12
        YEAR=$((YEAR-1))
        #
        fi
        #
        set `cal $MONTH ${YEAR}`
        shift $(($# - 1))
        YESTERDAY=$1
        #
        fi
        #
        YESTERDAY_=$YESTERDAY
        if [ $YESTERDAY -ne "0" ];
        then
        if [ $YESTERDAY -le 9 ];
        then
        #
        YESTERDAY_=${DAYS_[YESTERDAY]}
        #
        fi
        fi
        #
        TMONTH=${MONTHS[MONTH]}
        T_MONTH=${MONTH_[MONTH]}
        YEAR2=${YEAR##20}
        #
        # uncomment next line for debugging
        #
        echo ${WEEKDAY} ${YESTERDAY} ${TMONTH} ${YEAR}
        #
        #echo ${YESTERDAY}${MONTH}${YEAR2}
        #
        date=${YEAR}${T_MONTH}${YESTERDAY_}
        echo $date
        # ```
        0
        1. Hello,

          date=$(date)
          set $date

          date=$(date --date '1 days ago')
          set $datedate=$(date)
          set $date

          I would like to create a file with 2 time variables to make a comparison.
          diff -c /home/file_.$2$3 /home/file_.$2$3-1j > /home/test.log
          0

          1. DTE=`date --date '24 hours ago' '+%Y%m%d'`
            0
          2. ```html Use the following functions:

            function TimeToSecond {
            # ${1} = Optional date and time (YYYYMMDDHHMMSS); it defaults to current time
            ( typeset -r awk_date="\"y=\" substr(\$1,01,4) \"\\nm=\" substr(\$1,05,2) \"\\nd=\" substr(\$1,07,2)"
            typeset -r awk_time="\"h=\" substr(\$1,09,2) \"\\ni=\" substr(\$1,11,2) \"\\ns=\" substr(\$1,13,2)"
            if [ -z "${1}" ]
            then date +%Y%m%d%H%M%S
            else echo "${1}"
            fi |\
            awk "{ print ${awk_date} \"\\n\" ${awk_time} }"
            echo
            echo "m += 9"
            echo "if (m <= 11) {"
            print "\ty -= 1 }"
            echo "if (m > 11) {"
            print "\tm -= 12 }"
            print "(((((y-1)*1461+1)/4 - y/100 + y/400 + (m*153+2)/5 + d + 59)*24 + h)*60 + i)*60 + s")\
            | bc
            }

            function SecondsToTime {
            # ${1} = number of seconds since January 1st of year 0001
            ( echo "s = ${1}"
            echo "i = s/60"
            echo "s -= i*60"
            echo "h = i/60"
            echo "i -= h*60"
            echo "d = h/24"
            echo "h -= d*24"
            echo "d += 305"
            echo "b = d/146097"
            echo "d -= b*146097"
            echo "y = b*400"
            echo "b = d/36524"
            echo "if (b > 3) {"
            print "\tb = 3 }"
            echo "d -= b*36524"
            echo "y += b*100"
            echo "b = d/1461"
            echo "d -= b*1461"
            echo "y += b*4"
            echo "b = d/365"
            echo "if (b > 3) {"
            print "\tb = 3 }"
            echo "d -= b*365"
            echo "y += b"
            echo "m = (d*5+2)/153"
            echo "d -= (m*153+2)/5-1"
            echo "m += 3"
            echo "if (m > 12) {"
            print "\tm -= 12"
            print "\ty += 1 }"
            echo "y"
            echo "m"
            echo "d"
            echo "h"
            echo "i"
            echo "s"
            )\
            | bc | paste -s - | awk '{ printf "%04d%02d%02d%02d%02d%02d\n",$1,$2,$3,$4,$5,$6 }'
            } ```
            0