Generate comma separated dates for last seven days:
Considering current date is 2016-04-08, DATE variable value will be as follows:
We can use different date formats on a case to case basis. This thing comes in handy when we have to feed a date range to some reporting tool.
Generate comma separated dates for last month and from today to start of the day:
# Fetch the date 7 days ago
DATE=$(date -d '7 day ago' '+%Y-%m-%d');
# Generate comma-separated string for last 7 days
for i in {6..1}; do DATE=$DATE","$(date -d "$i day ago" '+%Y-%m-%d'); done
DATE=$(date -d '7 day ago' '+%Y-%m-%d');
# Generate comma-separated string for last 7 days
for i in {6..1}; do DATE=$DATE","$(date -d "$i day ago" '+%Y-%m-%d'); done
2016-04-01,2016-04-02,2016-04-03,2016-04-04,2016-04-05,2016-04-06,2016-04-07
We can use different date formats on a case to case basis. This thing comes in handy when we have to feed a date range to some reporting tool.
Generate comma separated dates for last month and from today to start of the day:
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; |