简体   繁体   English

我想显示当天1天前的日期

[英]I want to display the date of 1 day ago of current day

For every start my application it should take sysdate- 2 date. 对于我的应用程序,每次启动都需要sysdate-2日期。 i created shell script for that. 我为此创建了shell脚本。 But during month ends im facing the problem. 但是在月末,我面临着问题。 its not updating correctly. 它没有正确更新。

My script 我的剧本

a1=`date +%d`
a1=`expr $a1 - 2`
b1=`date +%m-%Y`
rm LastTopupDate.txt
echo $a1-$b1 >> Date.txt

help me out from this problem.? 帮助我摆脱这个问题。

Assuming GNU date , you can use 假设GNU date ,您可以使用

a1=$(date +%s)
b1=$(date +%m-%Y --date @$((a1-2*86400)))

Syntax for other implementations may be different, but the same idea applies: a1 is the date represented as a UNIX timestamp (number of seconds since a fixed epoch), and b1 is the date that occurs 2 days (86,400 seconds in a day) earlier. 其他实现的语法可能有所不同,但适用相同的想法: a1是表示为UNIX时间戳的日期(自固定时期以来的秒数), b1是早于2天(一天为86,400秒)的日期。

GNU date also provides a nicer shortcut: GNU date还提供了一个更好的快捷方式:

b1=$(date +%m-%Y --date '2 days ago')

Answering to the title question which is about yesterday's date, here is something that should work on systems which do not provide GNU date: 回答有关昨天日期的标题问题,以下内容应在不提供GNU日期的系统上起作用:

h=$(date +%H)
today=$(date +%d)

for i in -12 -6 +0 +6 +12; do
        gmth=$(TZ=UTC$i date +%H)
        gmttoday=$(TZ=UTC$i date +%d)
        if [ $gmttoday == $today ] && [ $gmth -gt 4 ]; then
                TZ=UTC$((i+24)) date +%d-%m-%Y
                break
        fi
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM