pathikrit
pathikrit

Reputation: 33439

Shell script: How to find what day of the year is today?

How do I tell what year of day is today e.g. if today was Mar 1 2008, it should reply 61.

Upvotes: 1

Views: 140

Answers (2)

glenn jackman
glenn jackman

Reputation: 246799

If you have GNU date,

$ date -d "Mar 1 2008" +%j
061

If you don't have GNU date, but you have a recent-ish version of Perl:

perl -MTime::Piece -le '
    print Time::Piece->strptime("Mar 1 2008", "%b %e %Y")->strftime("%j")
'

Upvotes: 1

cnicutar
cnicutar

Reputation: 182629

Try the %j format specifier:

$ date +%j
016

The standard says this about %j:

%j

Day of the year as a decimal number [001,366].

Upvotes: 3

Related Questions