Skip to content

Instantly share code, notes, and snippets.

@sarahbkim
Last active August 29, 2015 14:07
Show Gist options
  • Save sarahbkim/f4222dab265ba161e5da to your computer and use it in GitHub Desktop.
Save sarahbkim/f4222dab265ba161e5da to your computer and use it in GitHub Desktop.
getting date variables in bash
# set date variables
TODAY=`date --date= +%Y-%m-%d`
FIRST_DAY_PREVIOUS_MONTH=`date --date="-1 month" +%Y-%m-01`
LAST_DAY_PREVIOUS_MONTH=`date -d "-$(date +%d) days" +%Y-%m-%d`
#get english month
ENGLISH_MONTH=`date "+%B"`
# Get today's quarter and year
START_QUARTER=$(date +"%Y %m" | awk '{print ("CYQ" (int(($2-1)/3)+1) "'"'"'" $1)}')
# Trailing Thirty Start Date
TRAILING_THIRTY_START_DAY=$(date -d "$LAST_DAY_PREVIOUS_MONTH -29 day" -u +%Y-%m-%d)
# get the start month of each quarter
CURRENT_QUARTER=$(date +"%m" | awk '{print (int(($1-1)/3)+1)}')
if [ $CURRENT_QUARTER = '1' ]
then START_MONTH=$(date +"%Y" | awk '{print ($1 "-01-01")}')
elif [ $CURRENT_QUARTER = '2' ]
then START_MONTH=$(date +"%Y" | awk '{print ($1 "-04-01")}')
elif [ $CURRENT_QUARTER = '3' ]
then START_MONTH=$(date +"%Y" | awk '{print ($1 "-07-01")}')
elif [ $CURRENT_QUARTER = '4' ]
then START_MONTH=$(date +"%Y" | awk '{print ($1 "-10-01")}')
fi
@sarahbkim
Copy link
Author

Hey Sam,
You can replace the sh scripts below for the following date variable calculations (above):

  • english month (i.e. "January" from date)
  • quarter of a date
  • trailing 30 day start date
  • the first month of a quarter
#get english month
if [ $CURRENT_MONTH_NUMBER -eq '01' ]
    then TRAILING_THIRTY_START_DAY='2013-12-02'
elif [ $CURRENT_MONTH_NUMBER -eq '02' ]
    then TRAILING_THIRTY_START_DAY='2014-01-02'
elif [ $CURRENT_MONTH_NUMBER -eq '03' ]
    then TRAILING_THIRTY_START_DAY='2014-01-30'
elif [ $CURRENT_MONTH_NUMBER -eq '04' ]
    then TRAILING_THIRTY_START_DAY='2014-03-02'
elif [ $CURRENT_MONTH_NUMBER -eq '05' ]
    then TRAILING_THIRTY_START_DAY='2014-04-01'
elif [ $CURRENT_MONTH_NUMBER -eq '06' ]
    then TRAILING_THIRTY_START_DAY='2014-05-02'
elif [ $CURRENT_MONTH_NUMBER -eq '07' ]
    then TRAILING_THIRTY_START_DAY='2014-06-01'
elif [ $CURRENT_MONTH_NUMBER -eq '08' ]
    then TRAILING_THIRTY_START_DAY='2014-07-02'
elif [ $CURRENT_MONTH_NUMBER -eq '09' ]
    then TRAILING_THIRTY_START_DAY='2014-08-02'
elif [ $CURRENT_MONTH_NUMBER -eq '10' ]
    then TRAILING_THIRTY_START_DAY='2014-09-01'
elif [ $CURRENT_MONTH_NUMBER -eq '11' ]
    then TRAILING_THIRTY_START_DAY='2014-10-02'
elif [ $CURRENT_MONTH_NUMBER -eq '12' ]
    then TRAILING_THIRTY_START_DAY='2014-11-01'
fi

and

#get QTD calculation
#dec 10 2012 - adding 'START_QUARTER' for audio ad load stuff.  The date format is different, does not include 0 so using >= does not work.
if [ $FIRST_DAY_PREVIOUS_MONTH = '2012-07-01' ]
    then START_MONTH='2012-05-01'
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2012-08-01' ]
    then START_MONTH='2012-08-01'
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2012-09-01' ]
    then START_MONTH='2012-08-01'
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2012-10-01' ]
    then START_MONTH='2012-08-01'
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2012-11-01' ]
    then 
    START_MONTH='2012-11-01'
    START_QUARTER="Q4'2013"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2012-12-01' ]
    then 
    START_MONTH='2012-11-01'
    START_QUARTER="Q4'2013"

elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-01-01' ]
    then 
    START_MONTH='2012-11-01'
    START_QUARTER="Q4'2013"

elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-02-01' ]
    then 
    START_MONTH='2013-02-01'
    START_QUARTER="Q1'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-03-01' ]
    then 
    START_MONTH='2013-02-01'
    START_QUARTER="Q1'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-04-01' ]
    then 
    START_MONTH='2013-02-01'
    START_QUARTER="Q1'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-05-01' ]
    then 
    START_MONTH='2013-05-01'
    START_QUARTER="Q2'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-06-01' ]
    then 
    START_MONTH='2013-05-01'
    START_QUARTER="Q2'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-07-01' ]
    then 
    START_MONTH='2013-05-01'
    START_QUARTER="Q2'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-08-01' ]
    then 
    START_MONTH='2013-08-01'
    START_QUARTER="Q3'2014"

elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-09-01' ]
    then 
    START_MONTH='2013-08-01'
    START_QUARTER="Q3'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-10-01' ]
    then 
    START_MONTH='2013-08-01'
    START_QUARTER="Q3'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-11-01' ]
    then 
    START_MONTH='2013-11-01'
    START_QUARTER="Q4'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2013-12-01' ]
    then 
    START_MONTH='2013-11-01'
    START_QUARTER="Q4'2014"

#CYQ1'2014
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-01-01' ]
    then 
    START_MONTH='2014-01-01'
    START_QUARTER="CYQ1'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-02-01' ]
    then 
    START_MONTH='2014-01-01'
    START_QUARTER="CYQ1'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-03-01' ]
    then 
    START_MONTH='2014-01-01'
    START_QUARTER="CYQ1'2014"

#CYQ2'2014
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-04-01' ]
    then 
    START_MONTH='2014-04-01'
    START_QUARTER="CYQ2'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-05-01' ]
    then 
    START_MONTH='2014-04-01'
    START_QUARTER="CYQ2'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-06-01' ]
    then 
    START_MONTH='2014-04-01'
    START_QUARTER="CYQ2'2014"

#CYQ3'2014
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-07-01' ]
    then 
    START_MONTH='2014-07-01'
    START_QUARTER="CYQ3'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-08-01' ]
    then 
    START_MONTH='2014-07-01'
    START_QUARTER="CYQ3'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-09-01' ]
    then 
    START_MONTH='2014-07-01'
    START_QUARTER="CYQ3'2014"

#CYQ4'2014
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-10-01' ]
    then 
    START_MONTH='2014-10-01'
    START_QUARTER="CYQ4'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-11-01' ]
    then 
    START_MONTH='2014-10-01'
    START_QUARTER="CYQ4'2014"
elif [ $FIRST_DAY_PREVIOUS_MONTH = '2014-12-01' ]
    then 
    START_MONTH='2014-10-01'
    START_QUARTER="CYQ4'2014"


fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment