Linux 打印本周星期一的日期(在 bash 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6497525/
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
Print date for the monday of the current week (in bash)
提问by vehomzzz
I want to get a date in the format 'YYYYMMdd' (for example, today would be 20110627) format for the monday of the current week. From tomorrow through to Sunday I'd like to still print out Mondays (today's) date. Then Next week, repeat the process
我想以“YYYYMMdd”格式(例如,今天是 20110627)格式获取当前一周的星期一的日期。从明天到星期日,我仍然想打印出星期一(今天)的日期。然后下周重复这个过程
采纳答案by matchew
#monday
date -dmonday +%Y%m%d
#last monday
date -dlast-monday +%Y%m%d
#next monday
date -dnext-monday +%Y%m%d
#two mondays from now
date -d'monday+14 days' +%Y%m%d
#two mondays ago
date -d'monday-14 days' +%Y%m%d
#although, if you fancy yourself an Abraham Lincolin
date -d'monday-fortnight ago' +%Y%m%d #2 weeks ago
date -d'monday+fortnight' +%Y%m%d #2 weeks from now
#Monday Next Year
date -d'52+monday' +%Y%m%d
#However, Monday Last Year
date -d'52-monday' +%Y%m%d #DOES NOT WORK
#you can try a day other than monday
#and format this differently.
if a range is what your after you may need to do a few things
如果范围是您的目标,您可能需要做一些事情
#Tuesday to Sunday
#since today is monday, I'll use Tuesday
echo `date -dtuesday +%Y%m%d-``date -dnext-sunday +%Y%m%d`
which would output:
这将输出:
20110628-20110703
20110628-20110703
note this only works on GNU date
请注意,这仅适用于 GNU 日期
I have read that:
我读过:
Solaris version of date, which unable to support
-d
can be resolve with replacing sunfreeware.com version of date
Solaris版本的date,无法支持
-d
可以更换sunfreeware.com版本的date解决
回答by Femi
Try this to get the current Monday's date.
试试这个来获取当前星期一的日期。
wd=`date +%u`;
let wd=wd-1;
mon=`date --date="-$wd day" +%Y%m%d`;
回答by Sam Tsai
For those of us without GNU dates (like us OS Xers), we may have the "-v" parameter
对于我们这些没有 GNU 日期的人(比如我们 OS Xers),我们可能有“-v”参数
You can then do this:
然后你可以这样做:
# Most recent Monday
date -v -Mon
# Output as of this writing
Mon Jun 24 12:35:48 EDT 2013
date -v -Mon "+%Y%m%d"
# Outputs
20130624
This also seems to not be a problem if today is Monday, in my current case Thursday
如果今天是星期一,这似乎也不是问题,在我目前的情况下是星期四
# Today's date
date
# Outputs
Thu Jun 27 12:41:39 EDT 2013
# Most recent Thursday
date -v -Thu
# Outputs
Thu Jun 27 12:41:46 EDT 2013
回答by stef
I think this actually answers what was requested:
我认为这实际上回答了要求的内容:
date -d "next monday - 7 days"