skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search