Harness the Combinatoric Power of Command-Line Tools and Utilities
Dates with the Date Command
Published January 26, 2019
Explore the date command. Format dates and find dates in the future and in the past.
Transcript
Hi this is Brian and today we’re going to look at the date
command and how to
use it to display dates and look at dates in the future and in the past.
First, a small disclaimer: Some of the examples in this only work with the GNU version of date
found on Ubuntu and other Linux systems. If you’re on macOS, you won’t have this version, as macOS uses the BSD version of date with different options. You can install the GNU version of date along with many other tools using the Homebrew package manager to install the coreutils
package. That’s beyond the scope of this video though.o
Alright, let’s look at the date
command.
The date
command, by itself, displays today’s date and time:
date
You can pass options to the date command to show different outputs. This shows the month, date, and two-digit year:
date +%D
1/23/19
Let’s show the full day name, the full month, day, and four digit year. I’ll use quotes so I can put in a comma:
date "+%A, %B %d %Y"
Wednesday, January 23 2019
You can look at the man
pages for lots of other options on formatting with date.
You can also use the date command to look at dates in the future.
Suppose someone wants to meet with you next Monday. You’d like to know what date that is. You can use the Date command to get that answer pretty quickly:
date --date "next Monday"
You can also figure out the date 30 days from now:
date --date "30 days"
or even see the date 30 days in the past:
date --date "30 days ago"
Finally, if you use the date inside of command substitution, you can store it in a variable or use it in a script:
echo "Copyright $(date +%Y) Brian P. Hogan"
As you can see, you can do a lot with the date command. You can see the date and time, format the output, look ahead or behind days or even months, and use its output in creative ways.