Jason Stanley
Jason Stanley

Reputation: 457

Print date 5 days before in particular format

Im trying to get a particular format of a date that was 5 days ago.Im running bash on MacOS

Currently I have

date -v -5d
Wed  1 Jan 2020 09:32:07 EST

But the format Im looking for is

2020-01-01

Looking at some solutions I have something like

DATE=$(date +"%Y%m%d")
date -j -f %Y%m%d -v-5d "${DATE}" +%Y-%m-%d
2020-01-01

Would this be the most optimal way to do this ?

Upvotes: 1

Views: 418

Answers (2)

pascalre
pascalre

Reputation: 335

You can simply print the date with

date -v 5d +%m-%d-%Y
# output: 01-05-2020

Upvotes: 1

chepner
chepner

Reputation: 532093

Just like date -v -5d works without providing an explicit input date, so will

date -v -5d +%F  # %F is short for %Y-%m-%d

You just need to add the desired output format.

Upvotes: 1

Related Questions