Linux 使用`date`命令获取上个月、当前和下个月

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13168463/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:40:29  来源:igfitidea点击:

Using `date` command to get previous, current and next month

linuxbashunixdate

提问by greeness

I am using below to get previous, current and the next month under Ubuntu11.04:

我正在使用下面来获取Ubuntu11.04下的上个月、当前和下个月:

LAST_MONTH=`date +'%m' -d 'last month'`
NEXT_MONTH=`date +'%m' -d 'next month'`
THIS_MONTH=`date +'%m' -d 'now'`

It works well until today, the last day of October, 2012 (2012-10-31)

一直运行到今天,2012 年 10 月的最后一天 (2012-10-31)

I get below result as of now:

我现在得到以下结果:

$ date
Wed Oct 31 15:35:26 PDT 2012
$ date +'%m' -d 'last month'
10
$ date +'%m' -d 'now'
10
$ $ date +'%m' -d 'next month'
12

I suppose the outputs should be 9,10,11respectively.

我想输出应该是91011分别。

Don't understand why dateoutputs behave like this. What should be a good way to get consistant previous, currentand nextmonth instead?

不明白为什么date输出会这样。应该是什么让洽一个很好的方式previouscurrentnext一个月呢?

采纳答案by TomH

The problem is that datetakes your request quite literally and tries to use a date of 31st September (being 31st October minus one month) and then because that doesn't exist it moves to the next day which does. The datedocumentation (from info date) has the following advice:

问题是date从字面上理解您的请求并尝试使用 9 月 31 日(即 10 月 31 日减去一个月)的日期,然后因为不存在它移动到第二天。将date(从文件info date)有以下建议:

The fuzz in units can cause problems with relative items. For example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because 2003-06-31 is an invalid date. To determine the previous month more reliably, you can ask for the month before the 15th of the current month. For example:

 $ date -R
 Thu, 31 Jul 2003 13:02:39 -0700
 $ date --date='-1 month' +'Last month was %B?'
 Last month was July?
 $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
 Last month was June!

单位中的模糊可能会导致相关项目出现问题。例如,“2003-07-31 -1 月”的计算结果可能为 2003-07-01,因为 2003-06-31 是无效日期。为了更可靠地确定上个月,您可以要求在当月 15 号之前的月份。例如:

 $ date -R
 Thu, 31 Jul 2003 13:02:39 -0700
 $ date --date='-1 month' +'Last month was %B?'
 Last month was July?
 $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
 Last month was June!

回答by VIPIN KUMAR

the main problem occur when you don't have date --date option available and you don't have permission to install it, then try below -

当您没有可用的 date --date 选项并且您没有安装它的权限时,会出现主要问题,然后尝试以下操作-

Previous month
#cal -3|awk 'NR==1{print toupper(substr(,1,3))"-"}'
DEC-2016 
Current month
#cal -3|awk 'NR==1{print toupper(substr(,1,3))"-"}'
JAN-2017
Next month
#cal -3|awk 'NR==1{print toupper(substr(,1,3))"-"}'
FEB-2017

回答by estebanfallasf

If you happen to be using date in a MacOS environment, try this:

如果您碰巧在 MacOS 环境中使用 date,请尝试以下操作:

ST1:~ ejf$ date
Mon Feb 20 21:55:48 CST 2017
ST1:~ ejf$ date -v-1m +%m
01
ST1:~ ejf$ date -v+1m +%m
03

Also, I'd rather calculate the previous and next month on the first day of each month, this way you won't have issues with months ending the 30/31 or 28/29 (Feb/Feb leap year)

另外,我宁愿在每个月的第一天计算上个月和下个月,这样您就不会遇到结束 30/31 或 28/29(二月/二月闰年)的月份的问题

回答by guest

the following will do:

以下将做:

date -d "$(date +%Y-%m-1) -1 month" +%-m
date -d "$(date +%Y-%m-1) 0 month" +%-m
date -d "$(date +%Y-%m-1) 1 month" +%-m

or as you need:

或根据您的需要:

LAST_MONTH=`date -d "$(date +%Y-%m-1) -1 month" +%-m`
NEXT_MONTH=`date -d "$(date +%Y-%m-1) 1 month" +%-m`
THIS_MONTH=`date -d "$(date +%Y-%m-1) 0 month" +%-m`

you asked for output like 9,10,11, so I used the %-m

你要求像 9,10,11 这样的输出,所以我使用了 %-m

%m(without -) will produce output like 09,... (leading zero)

%m(没有 -)将产生像 09,...(前导零)的输出

this also works for more/less than 12 months:

这也适用于多于/少于 12 个月:

date -d "$(date +%Y-%m-1) -13 month" +%-m

just try

你试一试

date -d "$(date +%Y-%m-1) -13 month"

to see full result

查看完整结果