Home > Software engineering >  Get the date three days from today with BusyBox date
Get the date three days from today with BusyBox date

Time:07-06

I need to the date three days from today. On CentOS, I can run the below command.

date -I -d "-3 days"

Which outputs

2022-07-02

I need the same inside my Docker container which is running on Alpine Linux 3.14.6v.

When I execute the same command I am getting the error:

date: invalid date '-3 days'

Anyone knows the workaround for this?

CodePudding user response:

From StackExchange.com:

https://unix.stackexchange.com/questions/206540/date-d-command-fails-on-docker-alpine-linux-container

BusyBox/Alpine version of date doesn't support -d options, even if the help is exatly the same in the Ubuntu version as well as in others more fat distros.

To work with -d options you just need to add coreutils package

CodePudding user response:

A way to do it could be to use a bit of arithmetic on a timestamp, then translate the timestamp back into a date:

date -d "@$(( $(date  %s) - 3 * 24 * 60 * 60 ))"

Given

docker run --rm alpine:3.14 sh -c 'date; 
  date -d "@$(( $(date  %s) - 3 * 24 * 60 * 60 ))"'

It would yield:

Tue Jul  5 11:51:31 UTC 2022
Sat Jul  2 11:51:31 UTC 2022
  • Related