Last active
August 29, 2015 14:07
-
-
Save sarahbkim/f4222dab265ba161e5da to your computer and use it in GitHub Desktop.
getting date variables in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Sam,
You can replace the sh scripts below for the following date variable calculations (above):
and