如何在Bash中打印当前时间前一天的日期?


当前回答

也许改用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
date --date='-1 day'

date --date='-1 day'

date -d "yesterday" '+%Y-%m-%d'

or

date=$(date -d "yesterday" '+%Y-%m-%d')
echo $date

对不起,在Solaris系统上没有提到我。 因此,-date开关在Solaris bash上不可用。

我发现我可以得到之前的日期与小技巧的时区。

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE