1

I'm currently working on an ESXI and want to get the date X day ago I have this command line working on other linux :

now=`date +"%Y/%m/%d"`
earlier=`date -d "$now -15 days" +%d/%m/%Y`

but when i try to use it on my esxi server the line :

earlier=`date -d "$now -15 days" +%d/%m/%Y`

is not working, i get the error

date: invalid date '2018/01/30 -4 days'

So I tried to write the date in differents way like american format but still have the error.

the esxi version is 6.0.0

I have searched on internet but i didn't find anything.

Can someone know what is the problem ? Thank you.

edit: for those having the same problem i got the solution in the comments below

  • @tripleee looks interesting but it's for the last saturday, i'll try to figure out how to do it. thank you – user7394492 Jan 31 '18 at 10:50
  • The general approach will be the same, you have to use some external tool to calculate the time from the difference. Minus 15 days is -15 * 24 * 3600 seconds from now. – tripleee Jan 31 '18 at 10:52
  • i got it but the output is wrong for me, I only need something as YYYY/MM/DD – user7394492 Jan 31 '18 at 10:55
  • That's easy to influence with something like `+"%Y/%m/%d"` though your current command actually has a different format string. For computer-readable output, `+%F` is probably what you should use (`+%Y-%m-%d` if Busybox doesn't have `%F`). – tripleee Jan 31 '18 at 11:00
  • yes but the command you linked doesnt use +%F or +%Y-%m-%d it's using second to choose the day. – user7394492 Jan 31 '18 at 11:16
  • Exactly. You need to calculate the second, pass it in, and request the date format you want for the output with a `+%Y-%m-%d` or similar. – tripleee Jan 31 '18 at 11:17
  • i can add +%Y-%m-%d but i cant rid of the second because if i do i cant substract X days. Or maybe i can store the result in a global variable and the annoying part – user7394492 Jan 31 '18 at 11:26
  • So roughly like `date -d "1970.01.01-00:00:$(date +'%s - 15 * 24 * 60 * 60' | xargs expr)" +%Y-%m-%d` – tripleee Jan 31 '18 at 11:27
  • thank you so much, i tried to do this way but it didnt work because of wrong quote. Thank you again you really helped me – user7394492 Jan 31 '18 at 11:31
  • This is really just derived from the duplicate I nominated many comments ago. Please accept it. – tripleee Jan 31 '18 at 11:32

1 Answers1

5

It turns out this is busybox date. Busybox date is very limited in its abilities, but fortunately it supports (undocumentedly) the @seconds syntax:

date -d "@$(( $(busybox date +%s) - 86400 * 7 ))" +%d/%m/%Y

This of course requires that you have a modern version of bash. If this command doesn't work, try typing "bash" and if that works try typing the command again.

If you don't have modern bash...then do you have awk or bc installed? Python? Perl? Basically the tactical goal is to get date to spit out seconds, subtract 7 days worth of seconds, and then feed the output into date again to convert format.

Old

Try this command line. It avoids the $now reference which might confuse some versions of date.

date -d "15 days ago" +%d/%m/%Y

If this doesn't work, can you tell us which version of date you are using? date --version and/or date --help should provide the necessary information.

Seth Robertson
  • 30,608
  • 7
  • 64
  • 57