coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get the past Mon/Tue/.. not after the current date?


From: Eric Blake
Subject: Re: How to get the past Mon/Tue/.. not after the current date?
Date: Thu, 12 Aug 2021 13:01:13 -0500
User-agent: NeoMutt/20210205-687-0ed190

On Tue, Jul 20, 2021 at 07:36:15PM -0500, Peng Yu wrote:
> Hi,
> 
> $ date -d 'last Tue' +%Y-%m-%d
> 2021-07-13
> $ date +%Y-%m-%d
> 2021-07-20
> $ date -d 'last Mon' +%Y-%m-%d
> 2021-07-19
> 
> I want to get the last day of week not in the future. In the above
> example, I want to get this Tue (2021-07-20) instead the last Tue
> (2021-07-13). But for Mon, I want to get 2021-07-19. Is there a date
> string to get such output?

Sure - this (mostly) works:

$ date '+%a %Y-%m-%d' -d today
Thu 2021-08-12
$ date '+%a %Y-%m-%d' -d 'next Wed - 7 days'
Wed 2021-08-11
$ date '+%a %Y-%m-%d' -d 'next Thu - 7 days'
Thu 2021-08-12
$ date '+%a %Y-%m-%d' -d 'next Fri - 7 days'
Fri 2021-08-06

Note that 'date --debug' warns you that daylight savings may
interfere, and that you may also want to pin the time within the
starting day to noon to be sure you land within the expected day, even
in the presence of a 23- or 25-hour day.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org




reply via email to

[Prev in Thread] Current Thread [Next in Thread]