Created
May 3, 2018 08:52
-
-
Save piyusht007/c21680375da575a3f0e79254b7a73584 to your computer and use it in GitHub Desktop.
Shell Scripting - Generate date string values
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
# This script generate comma separated dates for last month in yyyy-mm-dd format. | |
# Ex: | |
# "2018-04-01,2018-04-02,2018-04-03,2018-04-04,2018-04-05,2018-04-06,2018-04-07, | |
# 2018-04-08,2018-04-09,2018-04-10,2018-04-11,2018-04-12,2018-04-13,2018-04-14, | |
# 2018-04-15,2018-04-16,2018-04-17,2018-04-18,2018-04-19,2018-04-20,2018-04-21, | |
# 2018-04-22,2018-04-23,2018-04-24,2018-04-25,2018-04-26,2018-04-27,2018-04-28, | |
# 2018-04-29,2018-04-30" | |
set `date +%m" "%Y` | |
CURMTH=$1 | |
CURYR=$2 | |
if [ $CURMTH -eq 1 ] | |
then | |
PRVMTH=12 | |
PRVYR=`expr $CURYR - 1` | |
else | |
PRVMTH=`expr $CURMTH - 1` | |
PRVYR=$CURYR | |
fi | |
if [ $PRVMTH -lt 10 ] | |
then PRVMTH="0"$PRVMTH | |
fi | |
LASTDY=`cal $PRVMTH $PRVYR | egrep "28|29|30|31" |tail -1 |awk '{print $NF}'` | |
FROM_DATE="$PRVYR-$PRVMTH-01" | |
TO_DATE="$PRVYR-$PRVMTH-$LASTDY" | |
# convert in seconds sinch the epoch: | |
start=$(date -d$FROM_DATE +%s) | |
end=$(date -d$TO_DATE +%s) | |
cur=$start | |
while [ $cur -le $end ]; do | |
# convert seconds to date: | |
REPORT_DATE=$REPORT_DATE$(date -d@$cur +%Y-%m-%d)","; | |
let cur+=24*60*60 | |
done | |
# Remove last comma (,) | |
REPORT_DATE=${REPORT_DATE::-1} | |
echo $REPORT_DATE; |
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
# This script generates the dates from today till the start of the month in yyyy-mm-dd format. | |
# Ex. If current date is 2018-05-03 then it will generate "2018-05-01,2018-05-02,2018-05-03". | |
set `date +%m" "%Y` | |
CURMTH=$1 | |
CURYR=$2 | |
CURRENT_DATE=`date +%d`; | |
FROM_DATE="$CURYR-$CURMTH-01" | |
TO_DATE="$CURYR-$CURMTH-$CURRENT_DATE" | |
# convert in seconds sinch the epoch: | |
start=$(date -d$FROM_DATE +%s) | |
end=$(date -d$TO_DATE +%s) | |
cur=$start | |
while [ $cur -le $end ]; do | |
# convert seconds to date: | |
REPORT_DATE=$REPORT_DATE$(date -d@$cur +%Y-%m-%d)","; | |
let cur+=24*60*60 | |
done | |
# Remove last comma (,) | |
REPORT_DATE=${REPORT_DATE::-1} | |
echo $REPORT_DATE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment