在 bash/unix 中获取上一个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16027776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:35:35  来源:igfitidea点击:

getting a previous date in bash/unix

bashshellunixsolarissolaris-10

提问by misguided

I am looking to get previous date in unix / shell script .

我希望在 unix/shell 脚本中获取上一个日期。

I am using the following code

我正在使用以下代码

date -d '1 day ago' +'%Y/%m/%d'

date -d '1 day ago' +'%Y/%m/%d'

But I am getting the following error.

但我收到以下错误。

date: illegal option -- d

date: illegal option -- d

As far as I've read on the inetrnet , it basically means I am using a older version of GNU. Can anyone please help with this.

就我在 inetrnet 上的阅读而言,这基本上意味着我使用的是旧版本的 GNU。任何人都可以帮忙解决这个问题。

Further Info

更多信息

unix> uname -a

unix> uname -a

SunOS Server 5.10 Generic_147440-19 sun4v sparc SUNW,Sun-Fire-T200

SunOS Server 5.10 Generic_147440-19 sun4v sparc SUNW,Sun-Fire-T200

Also The below command gives an error.

另外下面的命令给出了一个错误。

unix> date --version

unix> date --version

date: illegal option -- version
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]

采纳答案by devnull

Several solutions suggested here assume GNU coreutilsbeing present on the system. The following should work on Solaris:

此处建议的几种解决方案假定GNU coreutils存在于系统中。以下应该适用于 Solaris:

TZ=GMT+24 date +'%Y/%m/%d'

回答by kumarprd

try this:

尝试这个:

date --date="yesterday" +%Y/%m/%d

回答by Vladislav Ross

dtd="2015-06-19"
yesterday=$( date -d "${dtd} -1 days" +'%Y_%m_%d' )
echo $yesterday;

回答by Dan Pickard

you can use

您可以使用

date -d "30 days ago" +"%d/%m/%Y"

to get the date from 30 days ago, similarly you can replace 30 with x amount of days

要获取 30 天前的日期,同样您可以用 x 天数替换 30

回答by user3568717

In order to get 1 day back date using date command:

为了使用 date 命令获得 1 天的回溯日期:

date -v -1dIt will give (current date -1) means 1 day before .

date -v -1d它会给 (current date -1) 表示 1 天前。

date -v +1dThis will give (current date +1) means 1 day after.

date -v +1d这将给 (current date +1) 表示 1 天后。

Similarly below written code can be used in place of dto find out year,month etc

类似地,可以使用以下编写的代码代替d来找出年、月等

y-Year, m-Month w-Week d-Day H-Hour M-Minute
S-Second

y-Year, m-Month w-Week d-Day H-Hour M-Minute
S-Second

回答by Ravi Babu Mathi

the following script prints the previous date to the targetDate(specified Date or given date)

以下脚本将上一个日期打印到targetDate(指定日期或给定日期)

targetDate=2014-06-30
count=1
startDate=$( echo `date -d "${targetDate} -${count} days" +"%Y-%m-%d"`)
echo $startDate

回答by tripleee

SunOS ships with legacy BSD userland tools which often lack the expected modern options. See if you can get the XPG add-on (it's something like /usr/xpg4/bin/date) or install the GNU coreutilspackage if you can.

SunOS 附带了传统的 BSD 用户空间工具,这些工具通常缺乏预期的现代选项。看看您是否可以获得 XPG 附加组件(类似于/usr/xpg4/bin/date)或安装 GNUcoreutils软件包(如果可以)。

In the meantime, you might need to write your own simple date handling script. There are many examples on the net e.g. in Perl. E.g. this one:

同时,您可能需要编写自己的简单日期处理脚本。网上有很多例子,例如在 Perl 中。例如这个

vnix$ perl -MPOSIX=strftime -le 'print strftime("%Y%m", localtime(time-86400))'
201304

(Slightly adapted, if you compare to the one behind the link.)

(稍微调整一下,如果与链接后面的比较的话。)

回答by misguided

I have used the following workaround to get to the required solution .

我已使用以下解决方法来获得所需的解决方案。

timeA=$(date +%Y%m)
sysD=$(date +%d)
print "Initial sysD $sysD">>LogPrint.log
sysD=$((sysD-1))
print "Final sysD $sysD">>LogPrint.log
finalTime=$timeA$sysD

I hope this is useful for people who are facing the same issue as me.

我希望这对与我面临同样问题的人有用。

回答by akash malbari

$ date '+%m/%d/%Y' --- current date


$ TZ=Etc/GMT+24 date  '+%m/%d/%Y'  -- one dayprevious date

Use time zone appropriately

适当使用时区

回答by venki

Try This: gdate -d "1 day ago" +"%Y/%m/%d"

试试这个: gdate -d "1 天前" +"%Y/%m/%d"