Jaimin Joshi
Jaimin Joshi

Reputation: 83

Getting previous day's date in Unix

I am using the following code to assign previous day's date to a variable yesterday:

yesterday=`TZ=GMT+24 date +%Y%m%d`;
echo $yesterday;

The value is assigned correctly when I execute the command during early morning hours. But during night hours (around 9 PM), I am not getting the previous day date, but the same date as today.

My server is located in PDT timezeone. I tried "TZ=PDT+24" for assigning, but got the same result.

FYI, I am using Solaris 5.10.

What can be the reason for this bizarre situation?

Upvotes: 2

Views: 19844

Answers (4)

Anurag Choudhary
Anurag Choudhary

Reputation: 890

For Unix OS, this works fine for me

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

Upvotes: 1

peterh
peterh

Reputation: 19235

On Solaris simply use GNU date rather than the Solaris version. Access with gdate as opposed to date. Then you simply do

gdate -d'yesterday' +%Y%m%d

Your Solaris SysAdmin may have given you a crippled host where GNU coreutils (GNU date is part of that package) are not installed by default. Shame on him.

More info on what a Solaris host should look like on this link. The link has info for both Solaris 10 and Solaris 11.

Upvotes: 1

emkay
emkay

Reputation: 169

There is a special variable in date '-v' and you can add a -v -1d to retreive the previous day record. I believe it should work fine on Solaris as well. The PDT timezone might not be an issue.

so a command like date -v -1d would give you yesterday's date.

Upvotes: 0

Kent
Kent

Reputation: 195079

I cannot do an exact test on solaris. but on linux this works for getting last day(yesterday):

kent$ date -d'yesterday' +%Y%m%d
20130520

so you just type "yesterday", you don't have to do something special with date. It is cool, isn't it?

Upvotes: 5

Related Questions