在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")
和应该这样做=)
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfigif
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20