How to display date using date command in Linux

Use

date command in Linux is used to set or print the system time and date         

Syntax

date [OPTION]... [+FORMAT]

Example

$date

Output:
Tue Aug 27 09:54:25 IST 2019

Note: Your output will differ depending upon the date on which you are executing the command

Explanation: the date command prints the current date and time of the system in a fixed format string as above. The format string is Abbreviated_week_day abbreviated_month day_of_the_month time(hh:mm:ss) time_zone YYYY(year).

Using Options with date command in linux

-d : is used to display the output time by a STRING of your choice. In other words you want to see date and time of some other day.

Example:

            $date
–d “tomorrow”

Output: Wed Aug 28 10:10:16 DST 2019

Explanation: The output date is of the next day

More Examples with “-d” option

            $date –d “yesterday”

            $date –d “2 months ago”

            $date –d “2 months”

            $date –d “1 year ago”

            $date –d “5 August 2000”

            $date –d “5 Aug 2050”

-f : displays the date for the each STRING written in a file

Example: First, create a file file_date

            $touch
file_date

Next, write the following lines into file_date using an editor (nano or vi)

tomorrow
yesterday
5 aug 2020
Last, execute the command

Finally, run the date command usinf the -f option as           

$date –f file_date

Output:

Wed Aug 28 10:33:49 DST 2019

Mon Aug 26 10:33:49 DST 2019

Wed Aug  5 00:00:00 DST 2020       

Explanation: For each of the three line inside the file (file_date) a corresponding output has been generated.

-R : displays the output in RFC 5322 format i.e. Abbreviated_week_day comma day_of_the_month abbreviated_month YYYY(year) time(hh:mm:ss) time_difference_with_respect_to_GMT (e.g. +0530 in the output below means 5 hours 30 minutes ahead of GMT) .

Example:
           $date –R

Output:

            Tue, 27 Aug 2019 10:40:26 +0530

-r : displays the last modification time of the file passed as argument

Example:

            $date –r file_date

Output:

            Tue Aug 27 10:45:31 DST 2019

-s : is used to set/change the time

Example:

            $sudo date -s "20170815 0915"

Output:

            Tue Aug 15 09:15:00 DST 2017

Explanation: the date and time is set as per the string  “20170815 0915” where 2017 is the year, 08 is the month, 15 is the day, 09 is hour and 15 is minutes.

Displaying the output in FORMAT of your choice.

If you do not like the default output discussed above, the date command in linux can be modified using certain FORMAT sequences. The full list of sequences is given below.

Example:

            $date  +”%d %b %Y”

Output: 27 Aug 2019

Explanation: %d displays day of month (i.e. 27), %b displays abbreviated month name (i.e. Aug) and %Y displays full year (i.e. 2019). Similarly, for any other FORMAT the relevant sequences can be used.

More examples:

            $date +”%H:%M:%S”

Output: 10:26:29

            $date +”%T %A”

Output: 10:28:25 Tuesday

Sequence List

       %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

       %q     quarter of year (1..4)

       %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 (e.g., -0400)

       %:z    +hh:mm (e.g., -04:00)

       %::z   +hh:mm:ss (e.g., -04:00:00)

    

Optional Flags      

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

Practice questions on how to use date command in Linux

Q. What was the day on 1st Jan, 2000?

Solution: https://youtu.be/sxGezQ03–Y

Output: Saturday

Q. Calculate the number of seconds elapsed from epoch till a given date

Solution: https://youtu.be/sxGezQ03–Y

PPT on How to Use date command

Other Relevant Commands

4 thoughts on “How to display date using date command in Linux

  1. in the date section with -f option we cannot create file with touch command as we have append “tomorrow , yesterday , 5 aug 2020 ” . we can only append this words using cat command ” cat > file_date” . Please correct it as soon as possible as this might create problems for others .
    Thank you

    1. Its only mentioned to create the file using touch and not adding the content using touch.. That you can do with cat or any editor you want.

Leave a Reply

Your email address will not be published. Required fields are marked *