本周星期一的打印日期(以bash为单位) [英] Print date for the monday of the current week (in bash)

查看:104
本文介绍了本周星期一的打印日期(以bash为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以当年星期一的格式YYYYMMdd(例如今天将为20110627)格式获取日期。从明天到星期天,我还想打印星期一(今天)的日期。然后下周,重复这个过程

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

推荐答案

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

输出:


20110628-20110703

20110628-20110703

更多日期

注意这只适用于GNU日期

note this only works on GNU date

我已经读过:


Solaris版本的日期,无法
支持 -d 可以用
解决取代sunfreeware.com版本的
日期

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

这篇关于本周星期一的打印日期(以bash为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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