Using the command-line calendar and date functions in Linux

488 readers like this.
System statistics with sar and the /proc filesystem

ajmexico. Modified by Jason Baker. CC BY-SA 2.0.

I have always interested in historical dates and determining what actual day of the week an event occurred on. What day of the week was the Declaration of Independence signed? What day of the week was I born on? What day of the week did the 4th of July in 1876 occur on? 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 providing these fun factoids, though. Here are some easy examples of cal commands you can issue on the command line:

Display current calendar month: $ cal

November 2016      
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 2016      
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

       November 2016         
 Su  Mo  Tu  We  Th  Fr  Sa  
        306 307 308 309 310  
311 312 313 314 315 316 317  
318 319 320 321 322 323 324  
325 326 327 328 329 330 331  
332 333 334 335 

Display the current month, previous month, and next month: $ cal -3

 October               November              December        
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
                   1         1  2  3  4  5               1  2  3  
 2  3  4  5  6  7  8   6  7  8  9 10 11 12   4  5  6  7  8  9 10  
 9 10 11 12 13 14 15  13 14 15 16 17 18 19  11 12 13 14 15 16 17  
16 17 18 19 20 21 22  20 21 22 23 24 25 26  18 19 20 21 22 23 24  
23 24 25 26 27 28 29  27 28 29 30           25 26 27 28 29 30 31  
30 31 

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 print out 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 and edited or saved to a PDF and shared.

The date command in Linux can display the date in several formats, or to 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

Wed Nov 2 21:20:22 EDT 2016

You can also convert from one date format to another. For example, to convert to the date standard format, use: $date --date="11/30/16"

Wed Nov 30 00:00:00 EST 2016

The time can also be specified: $ date --date="December 1 2017 12:00:00"

Fri Dec 1 12:00:00 EST 2017

You can also 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.

User profile image.
Educator, entrepreneur, open source advocate, life long learner, Python teacher. M.A. in Educational Psychology, M.S. Ed. in Educational Leadership, Linux system administrator.

15 Comments

Great article, Don! One fun date trick I use in my script to pull last month's pictures off my phone:

CURRENT_DATE="$(date +%Y-%m-15) -1 month"
YYYYMM=$(date --date="${CURRENT_DATE}" +%Y%m)

It's pretty incredible what you can do with the date command by giving the right formatting arguments.

Hello Ben and happy holidays to you. Question about your script. 1.) Is this just a reminder or it set to pull your pictures off your phone.

2.) Is this set on to work on your phone if so what program are you using?

In reply to by bcotton

-I (capital i) is eq to '+%F' :)

# cp foo{,_$(date -I)}.txt
# cp foo{,_$(date '+%F')}.txt

// foo_yyyy-mm-dd.txt

Very useful article. Indeed, the date command, particularly the GNU date command is one of the most powerful open source utilities. One hidden treasure or feature of the date command is -d or --date option, where it accepts free format human readable date string.

What day was it exactly a year ago or one year in the future -
$ date -d 'last year'
Thu Dec 17 00:34:47 EST 2015
$ date -d 'next year'
Sun Dec 17 00:36:27 EST 2017

An hour ago in UTC -
$ env TZ=UTC date -d '1 hour ago'
Sat Dec 17 04:35:39 UTC 2016

And this -
$ date -d '10 years ago + 3 months + 2 days'
Mon Mar 19 01:39:36 EDT 2007

I have just blogged about it in my site - Practical uses of the GNU date command

found a mistake?

i'm using cal from util-linux 2.28.2 and that's not what cal -m February does.

If you check 'cal -m February' on Ubuntu 16.04LTS I think you'll find that it works. On Fedora it does not yield that result. I found that Fedora uses gnu.org and Ubuntu uses BSD.

In reply to by anon (not verified)

Anytime someone mentions `cal`, I am always reminded of the Calendar Act of 1750 which gave us this beauty:

$ cal 9 1752
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Great article and info!

This proves that everyday I can learn something valuable (and useful as well)!
By the way it's somehow implemented in Android (ok, easy catch)... and HP-UX, and AIX (both system V based).
Very handy tool for one like me that works with too many terminals cluttering my screens!

Thank you Don for a great Article. How long have you been working with Linux or UNIX. and how long did it take for you to master the command-line?

From Fedora
cal without any arguments, display the current month.

cal Options:
-1, --one show only a single month (default)
-3, --three show three months spanning the date
-n, --months show num months starting with date's month
-S, --span span the date when displaying multiple months
-s, --sunday Sunday as first day of week
-m, --monday Monday as first day of week
-j, --julian output Julian dates
-y, --year show the whole year
-Y, --twelve show the next twelve months
-w, --week[=] show US or ISO-8601 week numbers
--color[=] colorize messages (auto, always or never)
colors are enabled by default

-V, --version output version information and exit

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.