FactorPad
Faster Learning Tutorials

Linux date command summary with examples

date allows you to display or set the system time and date.
  1. Purpose - Learn what date is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with date.
  4. A tip - Finish off with one more insight.
Video by Paul Alan Davis, CFA, October 10, 2016
Updated: July 04, 2017
In this tutorial, 15 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / date command


The Linux date command

Beginner

Learn to find and set system date and time at the Linux command line.

Video Tutorial

Linux date command summary with examples (3:40)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our fifteenth word, or command to memorize is date from the category System.

date allows you to display or set the system time and date.

Recall from our last video (tutorial) we talked about answering questions, and the date will help us answer the when question.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The date command has 10 options and the most common argument is a date/time format which can be highly customized.

I suggest reviewing the manual page and the double-dash --h option because Linux is very particular about the format for printing out and adjusting dates and times.

The default without options is the current time. The option --date shows a time other than now, and --set is for setting time. And now you know how to do that.

So why is date an important command? Well, most of us already know what time it is, but date helps us print system time on reports. Also, accurately calendering jobs is dependent on you ensuring that the system time is correct. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go, type date to see date and time, in standard format.

$ date Mon Oct 10 12:49:43 PDT 2016

And to see the extensive list of formats, type double-dash date --h.

(first 25 lines trimmed) FORMAT controls the output. Interpreted sequences are: %% a literal % %a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbreviated month name (e.g., Jan) %B locale's full month name (e.g., January) %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 20) %d day of month (e.g., 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour, space padded ( 0..23); same as %_H %l hour, space padded ( 1..12); same as %_I %m month (01..12) %M minute (00..59) %n a newline %N nanoseconds (000000000..999999999) %p locale's equivalent of either AM or PM; blank if not known %P like %p, but lower case %r locale's 12-hour clock time (e.g., 11:11:04 PM) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %t a tab %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %U week number of year, with Sunday as first day of week (00..53) %V ISO week number, with Monday as first day of week (01..53) %w day of week (0..6); 0 is Sunday %W week number of year, with Monday as first day of week (00..53) %x locale's date representation (e.g., 12/31/99) %X locale's time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year %z +hhmm numeric time zone (e.g., -0400) %:z +hh:mm numeric time zone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) %Z alphabetic time zone abbreviation (e.g., EDT) By default, date pads numeric fields with zeroes. The following optional flags may follow '%': - (hyphen) do not pad the field _ (underscore) pad with spaces 0 (zero) pad with zeros ^ use upper case if possible # use opposite case if possible After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate representations if available, or O to use the locale's alternate numeric symbols if available. Examples: Convert seconds since the epoch (1970-01-01 UTC) to a date $ date --date='@2147483647' Show the time on the west coast of the US (use tzselect(1) to find TZ) $ TZ='America/Los_Angeles' date Show the local time for 9AM next Friday on the west coast of the US $ date --date='TZ="America/Los_Angeles" 09:00 next Fri' (last 4 lines trimmed)

Then Shift-PgUp to scroll, Shift-PgDn. Take a look at that. It's quite a list. Wow!

Next, let's use double-dash --date="yesterday" to show yesterday's date.

$ date --date="yesterday" Sun Oct 9 12:50:11 PDT 2016

And then last, let's see the date yesterday, using a plus and this format +%D.

$ date --date="yesterday" +%D 10/09/16

A Final Tip

Okay now you know how to use date. And you know the syntax for commands, options and arguments.

One last tip about the date command. I suggest reviewing the user manual for date and be careful when setting the system time. Next we'll cover another time-related command.

Okay, thanks for visiting today. I hope this was a fun introduction to the date command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Don't miss our next video tutorial. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline
  • To go back to the whereis command, hit Back.
  • For another time-related tutorial #16 on the touch command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / date command



 
 
Keywords:
linux date command
linux print date
linux print system time
linux print system date
linux set time
linux time
linux set date
shell date
linux today's date
linux date examples
linux date tutorial
set system time
linux print name
linux date syntax
linux date command summary