How to generate a year-quarter date from the command line

command linedate

I'm interested in outputting a representation of the current year-quarter, as well as the year-quarter for the previous month.

If today is 2012 January 1st, I'd like to get

2012q1

and

2011q4

as the respective outputs.

Best Answer

One (kinda ugly) solution, using BASH arithmetic evaluation and the GNU date command:

echo $(date +%Y)q$(( ($(date +%-m)-1)/3+1 ))
echo $(date -d "-1 month" +%Y)q$(( ($(date -d "-1 month" +%-m)-1)/3+1 ))

Note that the %-m prevents date from 0-padding, so this will still work for August and September.

Related Question