如何从UNIX中的给定日期获取最后一个星期日日期 [英] How do i get last sunday date from a given date in unix

查看:56
本文介绍了如何从UNIX中的给定日期获取最后一个星期日日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取给定日期(不是当前日期)作为输出的最后一个星期天日期作为输入

get last sunday date as output from a given date (not current date) as input

示例输入:2017年8月30日(%m-%d-%Y)

Example input: 08-30-2017 (%m-%d-%Y)

输出应在上周日:2017年8月27日

output should be last sunday: 08-27-2017

===

以下所有命令均以当前日期作为参考.但是我想提供参考日期作为输入,以获取上一个星期天.请通过命令帮助我.

All of the below commands use current day as reference .But i want to give the reference date as input to get last sunday. Please help me with the command.

日期+%m-%d-%Y -d"2017-09-10 -7天"

date +%m-%d-%Y -d "2017-09-10 -7 days"

日期+%m-%d-%Y -d最后一个太阳"

date +%m-%d-%Y -d "last Sun"

推荐答案

我希望以下解决方案可以为您提供帮助:

I hope this below solution can help you:

export day=08-30-2017
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).

这篇关于如何从UNIX中的给定日期获取最后一个星期日日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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