tcsh: print date 2 weeks ago in shell

一世执手 提交于 2019-12-12 10:26:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!