在shell脚本今天的日期,减去X天 [英] Today's date, minus X days in shell script

查看:245
本文介绍了在shell脚本今天的日期,减去X天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建三个变量,分别用于年,月,日和今日的日期减去天X数字。对于这个问题,我会选择天的随机量:222

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.

所以,如果:

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

我要的是在此之前222天。

What I want is 222 days before this.

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

编辑:需要222 工作天,而不是222天定期

Need 222 working days instead 222 regular days.

推荐答案

对于GNU 日期

For 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")


对于BSD 日期


For BSD date::

如果您使用的是OS X或者FreeBSD,使用下面的,而不是因为BSD日期不同,GNU日期:

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

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")

来源: BSD日期手册

注意:

庆典和许多其他语言,你不能以数字字符开头的变量名,所以我prefixed它们与 DATE_ 为您服务。

In bash and many other languages, you cannot start a variable name with a numerical character, so I prefixed them with date_ for you.

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

假设:不考虑法定节假日,因为那只是让远远超出了我可以在一个shell脚本帮助您范围:)

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

考虑222工作日:


    每周
  • 5个工作日内,即楼(5分之222)==44周

  • 44周*每周==7天308天

  • 额外的天吃剩的: 222%5 == 2

  • 所以 222个工作日== 310天定期

  • 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

不过,有一个陷阱!如果正规天数为 308 或一些多个 7 ,那么我们会一直很好,因为任何多7天前从一个工作日仍然是一个工作日。因此,我们需要考虑今天是周一或周二:

But, there is a catch! If the number of regular days is 308 or 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:


  • 如果今天是周一,我们会得到周六,我们希望周四

  • 如果今天是星期二,我们会得到星期天,我们希望周五

所以你看,我们需要一个额外的偏移的2天多,如果今天或者是周一或周二;让我们找到了,我们先出发前:

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:

#!/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")

和应该这样做=)

这篇关于在shell脚本今天的日期,减去X天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆