user4428391
user4428391

Reputation: 381

How can I get the 1st and last date of the previous month in a Bash script?

I have scheduled a Bash script to run on the 1st of the month but I need to create 2 variables in it with the 1st and last date of the previous month, whatever those may be.

Is it possible to do this using just Bash?

Upvotes: 25

Views: 40605

Answers (7)

Anthony Palmer
Anthony Palmer

Reputation: 972

This can be done in two lines, tweak date format to suit.

START_LAST_MONTH=$(date "+%F" -d "$(date +'%Y-%m-01') -1 month")
END_LAST_MONTH=$(date "+%F" -d "$START_LAST_MONTH +1 month -1 day");

#Test Code
echo START_LAST_MONTH=$START_LAST_MONTH
echo END_LAST_MONTH=$END_LAST_MONTH

Running gives:

START_LAST_MONTH=2018-09-01
END_LAST_MONTH=2018-09-30

Boundary Testing

 for TEST_DATE in 2018-03-31 2018-12-31 2019-01-01
 do
      START_LAST_MONTH=$(date "+%F" -d "$(date -d $TEST_DATE +'%Y-%m-01') -1 month")
      END_LAST_MONTH=$(date "+%F" -d "$START_LAST_MONTH +1 month -1 day");
      echo TEST_DATE=$TEST_DATE, START_LAST_MONTH=$START_LAST_MONTH, END_LAST_MONTH=$END_LAST_MONTH
 done

Output

 TEST_DATE=2018-03-31, START_LAST_MONTH=2018-02-01, END_LAST_MONTH=2018-02-28
 TEST_DATE=2018-12-31, START_LAST_MONTH=2018-11-01, END_LAST_MONTH=2018-11-30
 TEST_DATE=2019-01-01, START_LAST_MONTH=2018-12-01, END_LAST_MONTH=2018-12-31

Upvotes: 7

radio_tech
radio_tech

Reputation: 721

Unlike some answers, this will work for the 31st and any other day of the month. I use it to output unix timestamps but the output format is easily adjusted.

first=$(date --date="$(date +'%Y-%m-01') - 1 month" +%s)
last=$(date --date="$(date +'%Y-%m-01') - 1 second" +%s)

Example (today's date is Feb 14, 2019):

echo $first $last

1546300800 1548979199

To output in other formats, change final +%s to a different format such as +%Y-%m-%d or omit for default format in your locale.

In case you need, you can also back up an arbitrary number of months like this:

    # variable must be >= 1
    monthsago=23
    date --date="$(date +'%Y-%m-01') - ${monthsago} month"
    date --date="$(date +'%Y-%m-01') - $(( ${monthsago} - 1 )) month - 1 second"

Example output (today's date is Feb 15, 2019):

Wed Mar 1 00:00:00 UTC 2017
Fri Mar 31 23:59:59 UTC 2017

Upvotes: 29

CHL
CHL

Reputation: 21

TEST

first=`date -d "02/05/2018" +"%Y%m01"`
last=`date -d "02/05/2018 + 1 month-5 day" +"%Y%m%d"`

example

RUNDATE="20180207"
y="${RUNDATE:0:4}"
m="${RUNDATE:4:2}"
d="${RUNDATE:6:2}"
RUNDATE_START=`date -d "$m/$d/$y" +"%Y%m01"`
  RUNDATE_END=`date -d "$m/$d/$y + 1 month - $d day" +"%Y%m%d"`

result

20180201<br/>
20180228

Upvotes: 2

confirmator
confirmator

Reputation: 394

Due to the varying length of months, I think the most dependable way to do this is to base the calendar offsets from the first day of the month rather than any other arbitrary date and then subtract the number of days...

In the snippet below, you can set $TODAY to whatever date you need and $LAST_MONTH_START and $LAST_MONTH_END will end up containing the previous month's start and end dates:

TODAY=$(date '+%F') # or whatever YYYY-MM-DD you need
THIS_MONTH_START=$(date -d "$TODAY" '+%Y-%m-01')
LAST_MONTH_START=$(date -d "$THIS_MONTH_START -1 month" '+%F')
LAST_MONTH_END=$(date -d "$LAST_MONTH_START +1 month -1 day" '+%F')

Upvotes: 9

ctoepfer
ctoepfer

Reputation: 21

Example:

#!/bin/bash
for monthsback in 1 2 3 4 5 6 7 8 9 10 11 12
do
    monthsfwd=`expr $monthsback - 1`
    startdate=`date -d "-${monthsback} month -$(($(date +%d)-1)) days" +%Y-%m-%d`
    enddate=`date -d "-$(date +%d) days -${monthsfwd} month" +%Y-%m-%d`
    echo "$monthsback month(s) ago:"
    echo $startdate
    echo $enddate
    echo ""
done

Will output the first and last day from (monthsback) months ago:

2016-06-01 - 2016-06-30

2016-05-01 - 2016-05-31

The following will set two variables with the start and end of the previous month:

#!/bin/bash
monthsback=1
monthsfwd=`expr $monthsback - 1`
startdate=`date -d "-${monthsback} month -$(($(date +%d)-1)) days" +%Y-%m-%d`
enddate=`date -d "-$(date +%d) days -${monthsfwd} month" +%Y-%m-%d`

Upvotes: 2

LogicIO
LogicIO

Reputation: 623

You can try following date commands regardless of the day you are executing them to get first and last day of previous month

Firstday=`date -d "-1 month -$(($(date +%d)-1)) days"`
Lastday=`date -d "-$(date +%d) days"`

Upvotes: 15

user2046117
user2046117

Reputation:

If you're doing this on the 1st day of the month then you can use something like

first=$(date --date='-1 month')
last=$(date --date='-1 day')

But if you're running on another date then I guess you'll need to start from a known reference date.

Upvotes: 0

Related Questions