#!/bin/bash
## date-arithmetic in months;  Eugene Reimer  2008;
##
## USAGE EXAMPLES:
##	dateplusmonths  200812  -1		--produces 200811
##	dateplusmonths  200812  -13		--produces 200711
## USAGE:
##	dateplusmonths DATE NBRMONTHS [+FMT]	--first param can be YYYYMMDD or YYYYMM;  optional 3rd param same as +FORMAT to date-cmd, default: +%Y%m

DATE=$1;  N=$2;  FMT=+%Y%m; [ $# -eq 3 ] && FMT=$3					##FMT as optional 3rd param, default is +%Y%m
YYYY=${DATE:0:4};  MM=10#${DATE:4:2};  DD=01; [ ${#DATE} -ge 8 ] && DD=${DATE:6:2}	##split DATE into year month day  (beware of octal 08 09)
##echo "DATE:$DATE YYYY:$YYYY MM:$MM DD:$DD N:$N"					##debug
((MM += N))
while ((MM<1 ));do ((YYYY-=1, MM+=12)); done
while ((MM>12));do ((YYYY+=1, MM-=12)); done
date -d "$YYYY$(printf %02d $MM)$DD"  "$FMT"						##resulting date formatted as per FMT


exit
CONSIDER: instead of a FMT param, might be more useful to make output in same style as input (wrt DD being present)==??==
	FMT seems appropriate in my dateplusdays, but then it accepts any date-style supported by date-cmd, whereas this one doesn't, only YYYYMM[DD];
	note: tried writing this to accept anything accepted by date-cmd;  scrapped because it interprets missing DD as YYMMDD, rather than YYYYMM as needed here;
