Linux 脚本 - 日期操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6306289/
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
Linux Script- Date Manipulations
提问by Karthik
I will set one date variable(Say '08-JUN-2011') and I want to do some calculations based on that date namely,
1. Have to get the first day of the given day's month.
2. Previous date of the given date's month.
3. Last day of the given date's month.
我将设置一个日期变量(比如 '08-JUN-2011'),我想根据该日期进行一些计算,即
1. 必须获得给定月份的第一天。
2. 给定日期月份的前一天。
3. 给定日期月份的最后一天。
All I know is manipulating using the current system date and time but don't know how to implement with user defined date. I need this to be achieved using Linux shell script.
我所知道的是使用当前系统日期和时间进行操作,但不知道如何使用用户定义的日期来实现。我需要使用 Linux shell 脚本来实现这一点。
Any help will be appreciated.
任何帮助将不胜感激。
Thanks,
Karthik
谢谢,
卡西克
采纳答案by Gregg
Here's how to perform the manipulations using GNU date:
以下是如何使用 GNU 日期执行操作:
#!/bin/sh
USER_DATE=JUN-08-2011
# first day of the month
FIRST_DAY_OF_MONTH=$(date -d "$USER_DATE" +%b-01-%Y)
PREVIOUS_DAY=$(date -d "$USER_DATE -1 days" +%b-%d-%Y)
# last day of the month
FIRST_DAY_NEXT_MONTH=$(date -d "$USER_DATE +1 month" +%b-01-%Y)
LAST_DAY_OF_MONTH=$(date -d "$FIRST_DAY_NEXT_MONTH -1 day" +%b-%d-%Y)
echo "User date: $USER_DATE"
echo "1. First day of the month: $FIRST_DAY_OF_MONTH"
echo "2. Previous day: $PREVIOUS_DAY"
echo "3. Last day of the month: $LAST_DAY_OF_MONTH"
The output is:
输出是:
User date: JUN-08-2011
1. First day of the month: Jun-01-2011
2. Previous day: Jun-07-2011
3. Last day of the month: Jun-30-2011
回答by frankc
This is going to be convoluted in a shell script. You are better off using Date::Manip in perl, or something similar in another full-featured language. However, I can think of some ways to do this with the date command. First of all, you can use a --date parameter to set a starting point for date, like so:
这将在 shell 脚本中复杂化。你最好在 perl 中使用 Date::Manip,或者在另一种全功能语言中使用类似的东西。但是,我可以想出一些使用 date 命令执行此操作的方法。首先,您可以使用 --date 参数来设置日期的起点,如下所示:
$ date --date='08-JUN-2011' Wed Jun 8 00:00:00 EDT 2011
You can get the previous date like this:
您可以像这样获取上一个日期:
$ date --date='08-JUN-2011 -1 days' Tue Jun 7 00:00:00 EDT 2011
For the last day of the month, I would just walking back from 31 until date does not fail. You can check $? for that
在本月的最后一天,我会从 31 日返回,直到日期不会失败。你可以检查 $? 为了那个原因
$ date --date='31-JUN-2011';echo $? date: invalid date `31-JUN-2011' 1 $ date --date='30-JUN-2011';echo $? Thu Jun 30 00:00:00 EDT 2011 0
For the first day of the month...that is usually 01 :)
本月的第一天……通常是 01 :)
回答by shellter
As you're using Linux, hhopefully you have the GNU date utility available. It can handle almost any description of a relative date that you think of.
当您使用 Linux 时,希望您可以使用 GNU 日期实用程序。它几乎可以处理您想到的任何对相对日期的描述。
Here are some examples
这里有些例子
date --date="last month" +%Y-%m-%d
date --date="yesterday" +%Y-%m-%d
date --date="last month" +%b
x=$(date --date "10 days ago" +%Y/%m/%d)
To learn more about it see GNU Date examples
要了解有关它的更多信息,请参阅GNU 日期示例
Once you use it some, you can shortcut your information gathering with date --help
, which shows all the basic options (but is sometimes is hard to interpret.)
一旦你使用了它,你可以用 来快捷地收集信息date --help
,它显示了所有基本选项(但有时很难解释。)
I hope this helps.
我希望这有帮助。
回答by spoonyfork
$ date +%m/01/%Y
I came here looking for a way to get the first day of the current month.
我来这里是为了寻找获得当月第一天的方法。
回答by hroptatyr
Using dateutils' droundtool:
使用 dateutils 的dround工具:
Current month:
这个月:
$ dround today -1
2014-02-01
Previous month
前一个月
$ dround today -31
2014-01-31
Last day of current month:
当月的最后一天:
$ dround today +31
2014-02-28
Of course you can use a custom date instead of today
, the idea is to round down or up to the desired day-of-the month, e.g. the next first-of-the-month given 2010-10-04:
当然,您可以使用自定义日期而不是today
,其想法是向下或向上舍入到所需的日期,例如给定 2010-10-04 的下一个月初:
$ dround 2010-10-04 +1d
2010-11-01