Print date for the monday of the current week (in bash)
#monday
date -dmonday +%Y%m%d
#last monday
date -dlast-monday +%Y%m%d
#next monday
date -dnext-monday +%Y%m%d
#two mondays from now
date -d'monday+14 days' +%Y%m%d
#two mondays ago
date -d'monday-14 days' +%Y%m%d
#although, if you fancy yourself an Abraham Lincolin
date -d'monday-fortnight ago' +%Y%m%d #2 weeks ago
date -d'monday+fortnight' +%Y%m%d #2 weeks from now
#Monday Next Year
date -d'52+monday' +%Y%m%d
#However, Monday Last Year
date -d'52-monday' +%Y%m%d #DOES NOT WORK
#you can try a day other than monday
#and format this differently.
if a range is what your after you may need to do a few things
#Tuesday to Sunday
#since today is monday, I'll use Tuesday
echo `date -dtuesday +%Y%m%d-``date -dnext-sunday +%Y%m%d`
which would output:
20110628-20110703
More on Dates
note this only works on GNU date
I have read that:
Solaris version of date, which unable
to support-d
can be resolve with
replacing sunfreeware.com version of
date
Get monday of the currentweek with linux date
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
Bash- getting week start from date
Try:
$ date -d "$(date -d yesterday +%u) days ago"
Mon Aug 21 18:18:54 PDT 2017
How it works:
date -d yesterday +%u
gets yesterday's day of the week (1=Monday, 7=Sunday).date -d "$(date -d yesterday +%u) days ago"
returns the date for enough days ago to get the monday before yesterday.For example, since today is Sunday, yesterday's day of week is 6 (Saturday). 6 days ago is last monday.
If today was Monday, yesterday's day of the week would be Sunday which is 7 and "7 days ago" would be the monday before today.
Get Monday and Sunday etc.. for a week for any date as parameter in Unix
Try this:
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).
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.
How do I get the current week start date and end date in ksh
If you have Gnu or recent versions of date, this works.
date -dlast-sunday +%d%m%y
date -dnext-saturday +%d%m%y
Use Perl on Solaris because date command does not support this.
perl -MPOSIX -e '@DateComponents = localtime; print strftime "%d%m%y", localtime time - 86400*(($DateComponents[6]))'
perl -MPOSIX -e '@DateComponents = localtime; print strftime "%d%m%y", localtime time + 86400*(6-($DateComponents[6]))'
BASH: print the next monday of date
If GNU date
(or any other date
) accepts some variation of "monday after $DAYCHECK", I haven't seen it. I think you have to do the math.
$ day_of_week=$( date +%u --date $DAYCHECK) # 1 = Monday, ... 7 = Sunday
$ date --date "$DAYCHECK +$((8-day_of_week)) days"
If DAYCHECK
is already a Monday, you'll get the following Monday. If you want DAYCHECK
instead, you'll need to handle it separately:
$ if (( day_of_week == 1 )); then
> date --date "$DAYCHECK"
> else
> date --date "$DAYCHECK +$((8-day_of_week)) days"
> fi
Related Topics
Identify the Files Opened a Particular Process on Linux
Apt-Get Error: Sub-Process /Usr/Bin/Dpkg Returned an Error Code (1)
How to Determine If Code Is Running in Signal-Handler Context
How to Check Status of Urls from Text File Using Bash Shell Script
Bluetoothctl to Hcitool Equivalent Commands
How to Control Backlight by Terminal Command
Why Is My Bash Script Adding <Feff> to the Beginning of Files
How to Tell What a Linux Process Is Waiting For
Setting Limit to Total Physical Memory Available in Linux
Identify Other End of a Unix Domain Socket Connection
Remove Log Files Using Cron Job
Check If Rsync Command Ran Successful
How to Emulate the Raspberry Pi 2 on Qemu
Awk One Liner Select Only Rows Based on Value of a Column
How to Record What Process or Kernel Activity Is Using the Disk in Gnu/Linux