how to use unix date function to calculate quarter of the year -


this question has answer here:

in unix, using date command, know can extract information given date , iso-8601 so:

$ date --iso-8601 -d 20131229 2013-12-29 

iso week number:

$ date -d 20131229 +%v 52 

iso day of week:

$ date -d 20131229 +%u 7 

is there way calculate quarter of year?

there no date setting calculate quarter, have bit different way. formula calculate quarter assuming 1-3 month 1st quarter etc. calculate month use:

$(date -d 20130129 +%m) 

month 0 based convert integer use:

$((10#$(date -d 20130129 +%m))) 

then make number of months 0 based in order calculate 0 base quarter:

$(($((10#$(date -d 20130129 +%m))) - 1)) 

and calculate 0 based quarter , adjust quarter:

echo $(($(($((10#$(date -d 20130129 +%m))) - 1)) / 3 + 1)) 

so how (and test proof):

echo $(($(($((10#$(date -d 20130129 +%m))) - 1)) / 3 + 1)) result:1 echo $(($(($((10#$(date -d 20130228 +%m))) - 1)) / 3 + 1)) result:1 echo $(($(($((10#$(date -d 20130329 +%m))) - 1)) / 3 + 1)) result:1 echo $(($(($((10#$(date -d 20130429 +%m))) - 1)) / 3 + 1)) result:2 echo $(($(($((10#$(date -d 20130529 +%m))) - 1)) / 3 + 1)) result:2 echo $(($(($((10#$(date -d 20130629 +%m))) - 1)) / 3 + 1)) result:2 echo $(($(($((10#$(date -d 20130729 +%m))) - 1)) / 3 + 1)) result:3 echo $(($(($((10#$(date -d 20130829 +%m))) - 1)) / 3 + 1)) result:3 echo $(($(($((10#$(date -d 20130929 +%m))) - 1)) / 3 + 1)) result:3 echo $(($(($((10#$(date -d 20131029 +%m))) - 1)) / 3 + 1)) result:4 echo $(($(($((10#$(date -d 20131129 +%m))) - 1)) / 3 + 1)) result:4 echo $(($(($((10#$(date -d 20131229 +%m))) - 1)) / 3 + 1)) result:4 

Comments