如何在 Bash 脚本中获取上个月的第一个和最后一个日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27920201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the 1st and last date of the previous month in a Bash script?
提问by user4428391
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.
我已经安排了一个 Bash 脚本在当月的 1 号运行,但我需要在其中创建 2 个变量,其中包含上个月的第一天和最后一天,无论这些变量是什么。
Is it possible to do this using just Bash?
是否可以仅使用 Bash 来做到这一点?
回答by LogicIO
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"`
回答by radio_tech
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.
与某些答案不同,这适用于本月的 31 日和任何其他日期。我用它来输出 unix 时间戳,但输出格式很容易调整。
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):
示例(今天的日期是 2019 年 2 月 14 日):
echo $first $last
1546300800 1548979199
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.
要以其他格式输出,请将final 更改+%s
为不同的格式,例如+%Y-%m-%d
或省略您的语言环境中的默认格式。
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
示例输出(今天的日期是 2019 年 2 月 15 日):
Wed Mar 1 00:00:00 UTC 2017
Fri Mar 31 23:59:59 UTC 2017
回答by confirmator
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
为您需要的任何日期,$LAST_MONTH_START
并且$LAST_MONTH_END
最终将包含上个月的开始和结束日期:
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')
回答by Anthony Palmer
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
回答by CHL
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
回答by ctoepfer
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:
将输出 (monthsback) 几个月前的第一天和最后一天:
2016-06-01 - 2016-06-30
2016-06-01 - 2016-06-30
2016-05-01 - 2016-05-31
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`
回答by owen79
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.
但是如果你在另一个日期跑步,那么我想你需要从一个已知的参考日期开始。