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

查看:20
本文介绍了获取星期一和星期日等.对于任何日期作为 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天全站免登陆