QA Graphic

Date - Unix Command Line

The Unix Date command is a very handy tool to do date displays and calculations.

The basic of displaying a date:


You can format the output by simply passing a few parameters:

date +"%B %d, %Y" April 05, 2017

Common Display Formats

Standard Date date +"%A, %B %d, %Y" Wednesday, April 05, 2017
Standard Time date +"%I:%M %p" 03:29 PM
MySQL Format date +"%Y-%m-%d %H:%M:%S" 2017-04-05 13:21:13
United States format date +"%m/%d/%Y" 04/05/2017
European date format date +"%d %B, %Y" 05 April, 2017
ISO 8601 date format date +"%Y-%m-%d" 2017-04-05
ISO 8601 Combined Format date +"%Y-%m-%dT%R:%S" 2017-04-05T13:3252

Find Non-Release Weeks

At my work we do release every Thursday. However, if the Thursday is within 2 days of the end of the month we skip doing a release that week.

It would be handy to know all the weeks that we will not be shipping and let people know in advance. I thought it would be a handy thing for a Bash script.

Here's a simple script that does that. This will loop through all the Thursdays in 2017 and see if it follows the business rule. If it's not a release week print out the date.

for (( start = 1; start <= $max; start++ )) do
# Get the Next Thursday
check=$(date -v+"$start"w -v+1d +"%e") 
check_fmt=$(date -v+"$start"w -v+1d +"%B %e, %Y")
# Get the last day of the Month
lastday=$(date -v+"$start"w -v1d -v+1m -v-1d +"%e")
# Check the number of days to the End of the Month
daystoendofmonth=$(($lastday - $check));
# Apply the 2 Day Business Rule and echo the no release weeks
if [ "$daystoendofmonth" -lt "2" ]
echo "Don't release on: " $check_fmt

The above script is written to be run on a Wednesday.

When I run this script, I discovered:

  • Don't release on: June 29, 2017
  • Don't release on: August 31, 2017
  • Don't release on: November 30, 2017

Looks like we'll have a pretty quiet summer this year.

  Filed under the Bash category. - Permalink


Some useful Bash tips and tricks that I have learned over the years.