如何在Bash中打印当前时间前一天的日期?
当前回答
MAC OSX
昨天的日期:
date -v-1d +%F
其中1d定义今天减去1天。同样的,
date -v-1w +%F -表示前一周的日期
date -v-1m +%F -表示上个月的日期
如果你有gnu日期,
date --date="1 day ago"
更多信息:https://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html
其他回答
date +%Y:%m:%d|awk -vFS=":" -vOFS=":" '{$3=$3-1;print}'
2009:11:9
也许改用Perl ?
perl -e 'print scalar localtime( time - 86400 ) . "\n";'
或者使用nawk, (ab)使用/usr/bin/adb:
nawk 'BEGIN{printf "0t%d=Y\n", srand()-86400}' | adb
我也发现了这个…疯了!
/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);printf "0t%d=Y\n", $2-86400}' | adb
#!/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
不是很性感,但可以胜任:
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 --date='-1 day'