Pipe string to GNU Date for conversion - how to make it read from stdin?

Brian Beckett picture Brian Beckett · Jan 5, 2012 · Viewed 13.5k times · Source

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.

Answer

user2058460 picture user2058460 · Feb 10, 2013

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 -