从bash中的日期减去天数

时间:2022-08-12 19:44:09

I want to subtract "number of days" from a date in bash. I am trying something like this ..

我想从bash中的日期中减去“天数”。我正在尝试这样的事情..

echo $dataset_date #output is 2013-08-07

echo $date_diff #output is 2   

p_dataset_date=`$dataset_date --date="-$date_diff days" +%Y-%m-%d` # Getting Error

4 个解决方案

#1


32  

You are specifying the date incorrectly. Instead, say:

您错误地指定了日期。相反,说:

date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d

If you need to store it in a variable, use $(...):

如果需要将其存储在变量中,请使用$(...):

p_dataset_date=$(date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d)

#2


3  

To me, it makes more sense if I put the options outside (easier to group), in case I will want more of them.

对我来说,如果我把选项放在外面(更容易分组),这将更有意义,以防万一我想要更多。

date -d "$dataset_date - $date_diff days" +%Y-%m-%d

Where:

哪里:

 1. -d --------------------------------- options, in this case 
                                         followed need to be date 
                                         in string format (look up on $ man date)
 2. "$dataset_date - $date_diff days" -- date arithmetic, more 
                                         have a look at article by [PETER LEUNG][1]
 3. +%Y-%m-%d -------------------------- your desired format, year-month-day

#3


3  

Here is my solution:

这是我的解决方案:

echo $[$[$(date +%s)-$(date -d "2015-03-03 00:00:00" +%s)]/60/60/24]

It calculates number of days between now and 2015-03-03 00:00:00

它计算从现在到2015-03-03 00:00:00之间的天数

#4


0  

Below code gives you date one day lesser

下面的代码为您提供一天较短的日期

ONE=1
dataset_date=`date`
TODAY=`date -d "$dataset_date - $ONE days" +%d-%b-%G`
echo $TODAY

#1


32  

You are specifying the date incorrectly. Instead, say:

您错误地指定了日期。相反,说:

date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d

If you need to store it in a variable, use $(...):

如果需要将其存储在变量中,请使用$(...):

p_dataset_date=$(date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d)

#2


3  

To me, it makes more sense if I put the options outside (easier to group), in case I will want more of them.

对我来说,如果我把选项放在外面(更容易分组),这将更有意义,以防万一我想要更多。

date -d "$dataset_date - $date_diff days" +%Y-%m-%d

Where:

哪里:

 1. -d --------------------------------- options, in this case 
                                         followed need to be date 
                                         in string format (look up on $ man date)
 2. "$dataset_date - $date_diff days" -- date arithmetic, more 
                                         have a look at article by [PETER LEUNG][1]
 3. +%Y-%m-%d -------------------------- your desired format, year-month-day

#3


3  

Here is my solution:

这是我的解决方案:

echo $[$[$(date +%s)-$(date -d "2015-03-03 00:00:00" +%s)]/60/60/24]

It calculates number of days between now and 2015-03-03 00:00:00

它计算从现在到2015-03-03 00:00:00之间的天数

#4


0  

Below code gives you date one day lesser

下面的代码为您提供一天较短的日期

ONE=1
dataset_date=`date`
TODAY=`date -d "$dataset_date - $ONE days" +%d-%b-%G`
echo $TODAY