如何在Bash中打印当前时间前一天的日期?
当前回答
date --date='-1 day'
其他回答
简短回答(GNU格式):
date +%Y-%m-%d -d "-2 day"
如果你正在使用OSX,但你需要创建GNU兼容,首先安装coreutils
brew install coreutils
然后编辑您的个人资料:
#gnu coreutils first
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
重新启动你的终端,现在你就可以使用GNU格式了!
好吧,这是一个晚的答案,但这似乎是有效的!
YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`;
echo $YESTERDAY;
不是很性感,但可以胜任:
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)'
由“马丁·克莱顿”的答案形成。
date +%Y:%m:%d|awk -vFS=":" -vOFS=":" '{$3=$3-1;print}'
2009:11:9
#!/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