How can I get the 1st and last date of the previou

2020-03-01 06:49发布

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?

标签: bash shell
7条回答
何必那么认真
2楼-- · 2020-03-01 07:15

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`
查看更多
虎瘦雄心在
3楼-- · 2020-03-01 07:20

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
查看更多
疯言疯语
4楼-- · 2020-03-01 07:21

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.

查看更多
虎瘦雄心在
5楼-- · 2020-03-01 07:30

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')
查看更多
淡お忘
6楼-- · 2020-03-01 07:31

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
查看更多
叼着烟拽天下
7楼-- · 2020-03-01 07:34

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

查看更多
登录 后发表回答