shell-script: parse seconds with date
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 dat开发者_开发技巧e '1307284916'
).
Is there a way to get date
to work?
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
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
$
精彩评论