15

I want to get the date 3 minutes on the future. For example, if "now" is

01-Jan-70 00:00:00 GMT 

I want to get

01-Jan-80 00:03:00 GMT

How should I do it? I'm working with busybox linux.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
michelemarcon
  • 3,357
  • 10
  • 32
  • 37

3 Answers3

19

With GNU date you can do it as simple as this:

date --date="3min"

But busybox seems not so smart (yet). The only reliable solution I came up with using bb is:

busybox date -D '%s' -d "$(( `busybox date +%s`+3*60 ))"

(you don't need the busybox parts if there is no other date implementation present)

If you want a formatted output, you could add this

busybox date -D '%s' +"%y%m%d%H%" -d "$(( `busybox date +%s`+3*60 ))"
Anthon
  • 78,313
  • 42
  • 165
  • 222
rozcietrzewiacz
  • 38,754
  • 9
  • 94
  • 102
  • I have not found why yet but this does not seem to be compatible with latest's alpine with busybox: `docker run -it alpine ash -c 'busybox date -D "%s" -d "$(( "$(busybox date +%s)"+3*60 ))"'` returns `date: invalid date '1617211960'` – hugoShaka Mar 31 '21 at 17:30
  • 1
    The correct syntax seems to be ```busybox date -d@"$(( `busybox date +%s`+3*60 ))" ``` – hugoShaka Mar 31 '21 at 18:34
11

Working solution on alpine linux

date -d@"$(( `date +%s`+180))"
Peder
  • 111
  • 1
  • 2
1

I had to set the date for +2 hours on busybox OS on my router my command:

date "$(date +%m%d"$(( `date +%H`+2 ))"%M%Y.%S)"
user63180
  • 11
  • 1