2

我正在尝试使用 date 命令来获取上个月。当我在 5 月 31 日运行它时,它会返回 5 月 1 日,我期待着 4 月的一些事情。有没有更好的方法来做到这一点?

> date --version
 date (GNU sh-utils) 2.0
Written by David MacKenzie.

Copyright (C) 1999 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> date
Fri May 31 13:29:08 EDT 2013
> date --date='last month'
Wed May  1 13:29:15 EDT 2013

根据@fvu 的评论,我试过这个:

> date --date='first day last month'
Thu May  2 14:00:43 EDT 2013
> date --date='first day next month'
Tue Jul  2 14:00:52 EDT 2013

这不太奏效。虽然这样做了:

>date --date='last day last month'
Tue Apr 30 14:06:28 EDT 2013

猜我需要月份的定义

4

1 回答 1

1

我相信“上个月”的意思是 30 天前,而不是上个月。

date --date="$(date +%Y-%m-15) -1 month" 

是用于检测上个月的信息示例,因为 -1 月与 -30 天相同

于 2013-05-31T19:48:03.603 回答