Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

linux - Print date for the monday of the current week (in bash)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#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

More on Dates

note this only works on GNU date

I have read that:

Solaris version of date, which unable to support -d can be resolve with replacing sunfreeware.com version of date


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...