Using the command-line calendar and date functions in Linux


[ad_1]

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.

[ad_2]

Source link

,

Leave a Reply