Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print date for the monday of the current week (in bash)

Tags:

date

linux

bash

I want to get a date in the format 'YYYYMMdd' (for example, today would be 20110627) format for the monday of the current week. From tomorrow through to Sunday I'd like to still print out Mondays (today's) date. Then Next week, repeat the process

like image 648
vehomzzz Avatar asked Jun 27 '11 18:06

vehomzzz


People also ask

How do I print the current date in bash?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...

What is date +% s in bash?

date +%S. Displays seconds [00-59] date +%N. Displays in Nanoseconds. date +%T.

How do I get the current date in Linux terminal?

To display current date and time under Linux operating system using command prompt use the date command or timedatectl command. These commands can also display the current time / date in the given FORMAT. We can set the system date and time as root user too.

How to get current weekday in bash script?

To get current weekday in Bash script, use date command with the format that outputs only weekday. There are two different types of format in which we could get weekday from the date. They are In this tutorial, we will learn the syntax to get the current weekday in the above said formats, with examples.

How to display date and time in Bash format?

Date command in Bash Format Description date +%u Day of the week [1-7] 1 is Monday, 6 is ... date +%U Shows week number of the year [00-53] date +%Y Displays year YYYY [2021] date +%Z Displays Time zone 15 more rows ...

How do I calculate the number of days in Unix?

Calculating the number of days and even the number of seconds seem impractical, but thanks to the “date” command, it can easily be printed in terminal: #!/bin/bash. numberOfdays = `date + % j `. echo “The Current Day Number: “ $numberOfdays. numberOfseconds = `date + % s `.

How do you find the Monday of the week in Excel?

This formula needs to be able to pick the Monday of the week regardless of what day it is in that week. We can use the Weekday function as a way to determine how far away today’s date is from the Monday of that week. The Weekday () function returns the day of the week in terms of a number, where:


2 Answers

#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

like image 157
matchew Avatar answered Oct 02 '22 09:10

matchew


For those of us without GNU dates (like us OS Xers), we may have the "-v" parameter

You can then do this:

# Most recent Monday date -v -Mon  # Output as of this writing Mon Jun 24 12:35:48 EDT 2013  date -v -Mon "+%Y%m%d"  # Outputs 20130624 

This also seems to not be a problem if today is Monday, in my current case Thursday

# Today's date date  # Outputs Thu Jun 27 12:41:39 EDT 2013  # Most recent Thursday date -v -Thu  # Outputs Thu Jun 27 12:41:46 EDT 2013 
like image 22
Sam Tsai Avatar answered Oct 02 '22 09:10

Sam Tsai