Home > other >  Get monday of the currentweek with linux date
Get monday of the currentweek with linux date

Time:05-24

I am writing a bash script and I need the date of the Monday of the current week.

date -d monday

returns the correct date if it is Monday. And

date -d last-monday

returns the correct date at Tuesday until Sunday.

I can use an if-clause in the script but I am interested if there is an oneliner too?

CodePudding user response:

Assuming a week starts on Sunday, how about:

date -d "$(( 1 - $(date  %w) )) days"

CodePudding user response:

date -d "next-monday - 7 days"

date -d "$(($(date  %u) -1)) days ago"


# today
date   %Y%m%d
20220524

# Monday
$ date -d "next-monday - 7 days"  %Y%m%d
20220523
# or
$ date -d "$(($(date  %u) -1)) days ago"  %Y%m%d
20220523
  • Related