1

In tcsh on OpenBSD, I need to print a date two weeks ago.

E.g. if today is 2013-03-02, I need to have 2013-02-16 printed out.

cnst
  • 25,870
  • 6
  • 90
  • 122

2 Answers2

1

It doesn't seem like tcsh allows executing nested commands, so, it looks like ksh has to be used.

date +%Y-%m-%d ; \
sh -c 'date -r $(expr $(date +%s) - $(expr 60 \* 60 \* 24 \* 14)) +%Y-%m-%d'
2013-03-02
2013-02-16

We get the date in the number of seconds since Epoch in UTC, and calculate two weeks in seconds with expr, subtract, and pass these seconds back to date with the -r argument.

Not sure if there's a shorter solution, other than using 1209600 in place of $(expr 60 \* 60 \* 24 \* 14):

sh -c 'date -r $(expr $(date +%s) - 1209600) +%Y-%m-%d'
cnst
  • 25,870
  • 6
  • 90
  • 122
  • i have spent more than 10 mins to understand why it doesn't giving output in my shell.. then I saw your comment... :( – alamin Apr 28 '18 at 17:20
1

This should work ( It works on GNU date. I am not very familiar with OpenBSD. So not sure if it works for you )

date -d "now - 14 days" +%Y-%m-%d
  • 1
    No, it obviously doesn't work. But an interesting alternative for GNU/Linux users! – cnst Apr 19 '13 at 05:45