我想获取一个格式为“YYYYMMdd”的日期(例如今天是20110627)作为当前一周的星期一。从明天到星期天,我仍然想打印星期一(今天)的日期。然后,下周,重复该过程

最佳答案

#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.

如果范围是您的目标,那么您可能需要做一些事情
#Tuesday to Sunday
#since today is monday, I'll use Tuesday
echo `date -dtuesday +%Y%m%d-``date -dnext-sunday +%Y%m%d`

将输出:



More on Dates

请注意,这仅适用于GNU日期

我读过:

关于linux - 当前星期一的打印日期(以bash表示),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6497525/

10-12 21:20