Get the date (a day before current time) in Bash

后端 未结 17 1840
臣服心动
臣服心动 2020-11-30 17:54

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

相关标签:
17条回答
  • 2020-11-30 18:16
    yesterday=`date -d "-1 day" %F`
    

    Puts yesterday's date in YYYY-MM-DD format into variable $yesterday.

    0 讨论(0)
  • 2020-11-30 18:17
    #!/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
    
    0 讨论(0)
  • 2020-11-30 18:18

    date --date='-1 day'
    

    0 讨论(0)
  • 2020-11-30 18:19

    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.

    0 讨论(0)
  • 2020-11-30 18:22

    Well this is a late answer,but this seems to work!!

         YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`;
         echo $YESTERDAY;
    
    0 讨论(0)
提交回复
热议问题