在Unix上获取星期一和星期日等一周的任何日期作为参数 [英] Get Monday and Sunday etc.. for a week for any date as parameter in Unix

查看:57
本文介绍了在Unix上获取星期一和星期日等一周的任何日期作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取一周中星期一和星期日的日期?

How to get the date of Monday and Sunday in a week for a date?

这给出了上一个"星期一的日期:

This gives date for 'last' monday:

date -dlast-monday +%Y%m%d

我想传递日期作为参数,以查找该周的星期一和星期日.基本上,我想获得一周的星期日和星期一,任何日期,而不仅仅是上周一.

I want to pass a date as parameter to find the Monday and Sunday for that week. Basically, I want to get Sunday and Monday for a week, for ANY date, NOT only for last monday.

推荐答案

尝试一下:

export day=2013-10-01
date -d "$day -$(date -d $day +%w) days"

这将始终打印给定日期(或日期本身)之前的星期日.

This will always print the Sunday before the given date (or the date itself).

date -d "$day -$(date -d $day +%u) days"

这将始终打印给定日期之前的星期日(而不是日期本身).

This will always print the Sunday before the given date (and never the date itself).

对于星期一,您需要添加 +1天:

For Mondays you need to add + 1 day:

date -d "$day -$(date -d $day +%u) days + 1 day"

您还应该考虑要在哪个星期一或星期日(这个日期不太清楚).这还取决于您将星期日视为一周的第一天还是最后一天.

You should also consider what Monday or Sunday you want to get (this wasn't quite clear) for which date. This also depends on whether you consider the Sunday the first or the last day of the week.

这篇关于在Unix上获取星期一和星期日等一周的任何日期作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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