Pipe string to GNU Date for conversion - how to ma

2019-02-16 12:24发布

GNU Date lets you convert date strings like so:

$ date +"%d %m %Y" -d "yesterday"
  04 01 2012

Is it possible to pipe a date string to it for conversion? I've tried the obvious -d - like so:

$ echo "yesterday" | date +"%d %m %Y" -d -

but it prints today's date instead of yesterdays.

Is it possible to pipe values to it or doesn't it support that?

Thanks.

4条回答
霸刀☆藐视天下
2楼-- · 2019-02-16 12:36

You can use `command` or $(command) substitution:

date +"%d %m %Y" -d $(echo "yesterday")
查看更多
Melony?
3楼-- · 2019-02-16 12:40

Yes.

 echo "yesterday" | xargs date +"%d %m %Y" -d
查看更多
我命由我不由天
4楼-- · 2019-02-16 12:44

date -f tells it to do the same thing as -d except for every line in a file... you can set the filename to "-" to make it read from standard input.

echo "yesterday" | date +"%d %m %Y" -f -
查看更多
叛逆
5楼-- · 2019-02-16 12:52

Just to throw it in, in bash:

date +"%d %m %Y" -f <(echo yesterday)
查看更多
登录 后发表回答