{"id":5757,"date":"2024-06-12T01:37:00","date_gmt":"2024-06-12T05:37:00","guid":{"rendered":"https:\/\/www.both.org\/?p=5757"},"modified":"2024-06-10T15:43:52","modified_gmt":"2024-06-10T19:43:52","slug":"using-the-command-line-calendar-and-date-functions-in-linux","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=5757","title":{"rendered":"Using the command line calendar and date functions in Linux"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"5757\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>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?<\/p>\n\n\n\n<p>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&#8217;t tell you what year because she would not like to know that I&#8217;m telling people her age.)<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<p>Display current calendar month: <code>$ cal<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> June 2024        \nSu Mo Tu We Th Fr Sa  \n                   1  \n 2  3  4  5  6  7  8  \n 9 10 11 12 13 14 15  \n16 17 18 19 20 21 22  \n23 24 25 26 27 28 29  \n30                  <\/code><\/pre>\n\n\n\n<p>Display a calendar for a specific month: <code>$ cal -m February<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> February 2024      \nSu Mo Tu We Th Fr Sa  \n             1  2  3  \n 4  5  6  7  8  9 10  \n11 12 13 14 15 16 17  \n18 19 20 21 22 23 24  \n25 26 27 28 29        <\/code><\/pre>\n\n\n\n<p>Display a calendar with the Julian days: <code>$ cal -j<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>       June 2024           \n Su  Mo  Tu  We  Th  Fr  Sa  \n                        153  \n154 155 156 157 158 159 160  \n161 162 163 164 165 166 167  \n168 169 170 171 172 173 174  \n175 176 177 178 179 180 181  \n182                          <\/code><\/pre>\n\n\n\n<p>Display the current month, previous month, and next month: <code>$ cal -3<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    May 2024             June 2024             July 2024        \nSu Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  \n          1  2  3  4                     1      1  2  3  4  5  6  \n 5  6  7  8  9 10 11   2  3  4  5  6  7  8   7  8  9 10 11 12 13  \n12 13 14 15 16 17 18   9 10 11 12 13 14 15  14 15 16 17 18 19 20  \n19 20 21 22 23 24 25  16 17 18 19 20 21 22  21 22 23 24 25 26 27  \n26 27 28 29 30 31     23 24 25 26 27 28 29  28 29 30 31           \n                      30                                    <\/code><\/pre>\n\n\n\n<p>You can show the whole year with <code>$ cal -y<\/code>, or use <code>$ cal -jy<\/code> to display Julian dates beginning with 1 on January 1 and ending on December 31 with 365 or 366 if it&#8217;s a leap year. You can also figure out slightly more complicated dates with the related ncal command. For example,&nbsp;<code>$ ncal -e<\/code> displays the&nbsp;date of Easter in the&nbsp;current year.<\/p>\n\n\n\n<p>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 &gt; YearlyCalendar. txt. The text file can be opened in any text editor, edited, or saved as a PDF and shared.<\/p>\n\n\n\n<p>The date&nbsp;command in Linux can display the date in several formats or set the date on your computer&#8217;s Linux operating system. The date&nbsp;command can be combined in shell scripts to, for example, easily append a date to file you are editing.&nbsp;Along with the calendar date, the time can also be specified. Here are a few examples.<\/p>\n\n\n\n<p>You can display today&#8217;s date with $date<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Fri Jun  7 10:49:44 AM EDT 2024<\/code><\/pre>\n\n\n\n<p>You can convert from one date format to another. For example, to convert to the date standard format, use: <code>$date --date=\"6\/7\/2024\"<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Fri Jun  7 12:00:00 AM EDT 2024<\/code><\/pre>\n\n\n\n<p>The time can be specified: <code>$ date --date=\"June 7 2024 12:00:00\"<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Fri Jun  7 12:00:00 PM EDT 2024<\/code><\/pre>\n\n\n\n<p>You can specify the date format. For example, for a YYYY-Mo-Day format, use <code>$ date +%F<\/code>, or given even more precision by specifying exact details to display, for example <code>$ date +\"%y-%m-%d\"<\/code>. You can direct time format as well: <code>$ date +%H<\/code> displays the current hour in 24-hour format, while <code>$ date +%I<\/code> will give it to you in 12-hour format.<\/p>\n\n\n\n<p>The date has a few shortcuts as well, for example, <code>$ date --date \"next monday\"<\/code> or <code>$ date --date \"yesterday\"<\/code>, which can be useful in a scripting context. Working with the date can be particularly helpful when writing Bash scripts, for example, <code>$ echo \"Today is $(date)\"<\/code> will output <code>Today is Tue Dec 6 2016 15:53:41 2016<\/code>. Or easily create backups of files by appending the date, for example, <code>$ cp foo.txt \"foo.txt.$(date +%F)\"<\/code> to add today&#8217;s date.<\/p>\n\n\n\n<p>To learn more or to see further examples of the date command, check out the&nbsp;<a href=\"http:\/\/www.gnu.org\/software\/coreutils\/manual\/html_node\/Examples-of-date.html#Examples-of-date\">Gnu Coreutils<\/a>&nbsp;documentation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p> 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?<\/p>\n","protected":false},"author":32,"featured_media":3857,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[149,100,5],"tags":[],"class_list":["post-5757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","category-command-line","category-linux"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5757"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5757\/revisions"}],"predecessor-version":[{"id":5766,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/5757\/revisions\/5766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3857"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}