shell-script: parse seconds with date

2019-07-27 14:32发布

I want to get the date +%m/%d/%Y a different number of days ago.

$(( $(date +%s)- 259200)) gives me the seconds of 3 days ago. The -d parameter doesn't accept the seconds to parse it in the format I want 05/06/2011 (error message: date: invalid date '1307284916').

Is there a way to get date to work?

标签: shell
2条回答
Deceive 欺骗
2楼-- · 2019-07-27 14:54

On MacOS X, you can use date -r 1307284916 +%m/%d/%Y to get the result you want. If you are not on a BSD-derived system like MacOS X (e.g. if you are using Linux), there doesn't seem to be a simple analogue of date -r. And traditional versions of Unix usually don't even provide the facilities of GNU's date.

If you get really stuck, contact me (see my profile) for a program timestamp:

$ timestamp -T '%m/%d/%Y' 1307284916
1307284916 = 06/05/2011
$ timestamp -n -T '%m/%d/%Y' 1307284916
06/05/2011
$ timestamp -T '%m/%d/%Y' 1307284916  $(( $(date +%s)- 259200))   
1307284916 = 06/05/2011
1307288704 = 06/05/2011
$
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-27 15:09

To get date on linux to accept a simple timestamp for -d, prefix the number with the @ sign!

~% date +%s
1307548153
~% date -d @1307548153
Wed Jun  8 16:49:13 IST 2011
查看更多
登录 后发表回答