Using the command line calendar and date functions in Linux
I have always been interested in historical dates and determining the actual day of the week an event occurred. What day of the week was the Declaration of Independence signed? What day of the week was I born? What day of the week did the 4th of July in 1876 occur? I know that you can use search engines to answer many of these questions. But did you know that the Linux command line can supply those answers, too?
July 4, 1776, was a Thursday. July 4 in 1876 was a Tuesday. My mom is celebrating her birthday soon and I know that she was born on Saturday, November 6. (I can’t tell you what year because she would not like to know that I’m telling people her age.)
The Linux date and calendar commands can do far more than just provide these fun factoids, though. Here are some easy examples of cal commands you can issue on the command line:
Display current calendar month: $ cal
June 2024
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Display a calendar for a specific month: $ cal -m February
February 2024
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29
Display a calendar with the Julian days: $ cal -j
June 2024
Su Mo Tu We Th Fr Sa
153
154 155 156 157 158 159 160
161 162 163 164 165 166 167
168 169 170 171 172 173 174
175 176 177 178 179 180 181
182
Display the current month, previous month, and next month: $ cal -3
May 2024 June 2024 July 2024
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
You can show the whole year with $ cal -y
, or use $ cal -jy
to display Julian dates beginning with 1 on January 1 and ending on December 31 with 365 or 366 if it’s a leap year. You can also figure out slightly more complicated dates with the related ncal command. For example, $ ncal -e
displays the date of Easter in the current year.
Like most command-line tools, the calendar tool is composable with pipes or other functions. If you would like a printout of the entire year, then pipe the calendar command to a text file, you can simply run $ cal 2016 > YearlyCalendar. txt. The text file can be opened in any text editor, edited, or saved as a PDF and shared.
The date command in Linux can display the date in several formats or set the date on your computer’s Linux operating system. The date command can be combined in shell scripts to, for example, easily append a date to file you are editing. Along with the calendar date, the time can also be specified. Here are a few examples.
You can display today’s date with $date
Fri Jun 7 10:49:44 AM EDT 2024
You can convert from one date format to another. For example, to convert to the date standard format, use: $date --date="6/7/2024"
Fri Jun 7 12:00:00 AM EDT 2024
The time can be specified: $ date --date="June 7 2024 12:00:00"
Fri Jun 7 12:00:00 PM EDT 2024
You can specify the date format. For example, for a YYYY-Mo-Day format, use $ date +%F
, or given even more precision by specifying exact details to display, for example $ date +"%y-%m-%d"
. You can direct time format as well: $ date +%H
displays the current hour in 24-hour format, while $ date +%I
will give it to you in 12-hour format.
The date has a few shortcuts as well, for example, $ date --date "next monday"
or $ date --date "yesterday"
, which can be useful in a scripting context. Working with the date can be particularly helpful when writing Bash scripts, for example, $ echo "Today is $(date)"
will output Today is Tue Dec 6 2016 15:53:41 2016
. Or easily create backups of files by appending the date, for example, $ cp foo.txt "foo.txt.$(date +%F)"
to add today’s date.
To learn more or to see further examples of the date command, check out the Gnu Coreutils documentation.