getting a previous date in bash/unix

喜欢而已 提交于 2019-11-28 04:12:41

问题


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

I am using the following code

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

But I am getting the following error.

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.

Further Info

unix> uname -a

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

Also The below command gives an error.

unix> date --version

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

回答1:


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

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



回答2:


try this:

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



回答3:


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



回答4:


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




回答5:


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

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

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

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

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




回答6:


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

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



回答7:


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 coreutils package if you can.

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:

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

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




回答8:


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.




回答9:


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


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

Use time zone appropriately




回答10:


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




回答11:


Problem

You are using backticks, rather than single quotes, for your arguments. You may also not be using GNU date, or a version of date that supports the flag you are using.

Solution

Quote your arguments properly. For example:

$ date -d '1 day ago' +'%Y/%m/%d'
2013/04/14


来源:https://stackoverflow.com/questions/16027776/getting-a-previous-date-in-bash-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!