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