问题
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.
回答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'
回答2:
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
来源:https://stackoverflow.com/questions/15183127/tcsh-print-date-2-weeks-ago-in-shell