bash 今天的日期,在 shell 脚本中减去 X 天

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

Today's date, minus X days in shell script

bashsh

提问by gcubed

I need to create three variables, each for Year, Month, and Day for Today's date, minus X number of days. For this question I'll choose a random amount of days: 222.

我需要创建三个变量,每个变量代表今天的年、月和日,减去 X 天数。对于这个问题,我将选择一个随机天数:222。

So if:

因此,如果:

TodayYear=`date +%Y`
TodayMonth=`date +%m`
TodayDay=`date +%d`

What I want is 222 days before this.

我想要的是在此之前的 222 天。

222days_before_TodayYear=???
222days_before_TodayMonth=???
222days_before_TodayDay=???

Edit: Need 222 workingdays instead 222 regular days.

编辑:需要 222 个工作日而不是 222 个正常工作日。

回答by sampson-chen

For GNU date:

对于 GNU date

date_222days_before_TodayYear=$(date --date="222 days ago" +"%Y")
date_222days_before_TodayMonth=$(date --date="222 days ago" +"%m")
date_222days_before_TodayDay=$(date --date="222 days ago" +"%d")


For BSD date::

对于 BSD date::

If you are using OS X or FreeBSD, use the following instead because BSD date is different from GNU date:

如果您使用的是 OS X 或 FreeBSD,请改用以下内容,因为 BSD 日期与 GNU 日期不同:

date_222days_before_TodayYear=$(date -j -v-222d +"%Y")
date_222days_before_TodayMonth=$(date -j -v-222d +"%m")
date_222days_before_TodayDay=$(date -j -v-222d +"%d")

Source: BSD date manual page

来源:BSD 日期手册页

Note:

笔记:

In bashand many other languages, you cannot start a variable name with a numerical character, so I prefixed them with date_for you.

bash许多其他语言中,您不能以数字字符开头变量名,因此我date_为您添加了前缀。



Second Update:New requirement - Using 222 Working days instead of 222 Regular days:

第二次更新:新要求 -使用 222 个工作日而不是 222 个正常工作日:

(Assumption:Not considering statutory holidays, because that just gets far beyond the scope of what I can help you with in a shell script:)

假设:不考虑法定假日,因为这远远超出了我在 shell 脚本中可以帮助您的范围:)

Consider 222 working days:

考虑 222 个工作日:

  • 5 working days per week, that is floor(222/5) == 44 weeks
  • 44 weeks * 7 days per week == 308 days
  • Extra days leftover: 222 % 5 == 2
  • Therefore 222 working days == 310 regular days
  • 每周 5 个工作日,即 floor(222/5) == 44 weeks
  • 44 weeks * 7 days per week == 308 days
  • 剩余天数: 222 % 5 == 2
  • 所以 222 working days == 310 regular days

But, there is a catch! If the number of regular days is 308or some multiple of 7, then we would have been fine, because any multiple of 7-days ago from a working day is still a working day. So we need to consider whether today is a Monday or a Tuesday:

但是有一个问题!如果常规天数是308或 的倍数7,那么我们就可以了,因为 7 天前的任何一个工作日的倍数仍然是一个工作日。所以我们需要考虑今天是星期一还是星期二:

  • If today is a Monday, we'd get Saturday where we wanted Thursday
  • If today is a Tuesday, we'd get Sunday where we wanted Friday
  • 如果今天是星期一,我们会在星期六到达我们想要的星期四
  • 如果今天是星期二,我们会在星期天得到我们想要的星期五

So you see we need an additional offset of 2 more days if today is either Monday or Tuesday; so let's find that out first before we proceed:

所以你看,如果今天是星期一或星期二,我们需要额外的 2 天的抵消;所以在我们继续之前,让我们先找出来:

#!/bin/bash

# Use 310 days as offset instead of 222
offset=310
# Find locale's abbreviated weekday name (e.g., Sun)
today=$(date -j +"%a")
# Check for Mon/Tue
if [[ "$today" == "Mon" ]] || [[ "$today" == "Tue" ]]; then
     offset=$((offset+2))
fi

date_222_working_days_before_TodayYear=$(date -j -v-${offset}d +"%Y")
date_222_working_days_before_TodayMonth=$(date -j -v-${offset}d +"%m")
date_222_working_days_before_TodayDay=$(date -j -v-${offset}d +"%d")

And that should do it =)

应该这样做 =)

回答by Ashish K

You can get exact past date from the following in bash

您可以在 bash 中从以下内容中获得确切的过去日期

Number=222
current_date=$(date +%Y%m%d)
past_date=$(date -d "$current_date - $Number days" +%Y%m%d)
echo "$current_date\t$past_date"

Hope this helps !

希望这可以帮助 !

回答by chrisaycock

date '+%Y' --date='222 days ago'

回答by djhaskin987

epoch=$(( `date '+%s'` - ( 24 * 60 * 60 * 222 ) ))
year=`date -d "@$epoch" '+%Y'`
month=`date -d "@$epoch" '+%m'`
day=`date -d "@$epoch" '+%d'`

Should do the trick.

应该做的伎俩。

回答by ThatComputerGuy

I would say easier solution would be

我会说更简单的解决方案是

222days_before_TodayYear = $(date -v -222d +%Y)
222days_before_TodayMonth = $(date -v -222d +%m)
222days_before_TodayDay = $(date -v -222d +%d)