Linux date command tutorial

Linux date command is used to set and display date and time in the command line terminal. It is quite an interesting command which allows user to customize the result format when viewing the system date. If we view date command manual page, it says that Linux date command prints or set the system date and time. That means other than displaying date and calendar, date command also capable of managing date and time for Linux system.

Here is Linux date command information as you can find it in the manual page:

NAME
date - print or set the system date and time

SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
Display the current time in the given FORMAT, or set the system date.

From the synopsis above, we can see that Linux date command supports different format to display date and also provides the option to use utc (Universal Time Coordinated) or universal time (GMT- Greenwich Mean Time). Here are explanation and the complete list of options used in the synopsis example above:

  • OPTION - option to customize date command.

  • FORMAT - controls the output.

  • -u - print or set Coordinated Universal Time.

  • MM - month.

  • DD - day within month.

  • hh - hour.

  • mm - minute.

  • CC - first two digits of year.

  • YY - last two digits of year.

  • ss - second

Note: If an argument starts with +, date prints the `date' prints the current date and time (or the date and time specified by the `--date' in the format defined by that argument. CC, YY and ss are optional.

Linux date command view date and time examples

When issuing date command without any option in Slackware Linux, it displays locale's date and time plus timezone information:

[email protected]:~$ date
Thu Jul 2 11:21:27 MYT 2009

If we want to check current date and time, invoke date from command line terminal will give us the information we need. If you feel uncomfortable or annoying with the date output format, you can use date command with -R option to display date and time in RFC 2822 format. See date command example below:

[email protected]:~$ date -R
Thu, 02 Jul 2009 14:15:21 +0800

To view Coordinated Universal Time (UTC), use date command with -u option. You'll see a result like the example below:

[email protected]:~$ date -u
Thu Jul 2 06:22:50 UTC 2009

To view date and time output in RFC 3339 format use --rfc-3339 option. Specify '--rfc-3339=date' to display date-only output or '--rfc-3339=ns' to show precise time to nanoseconds. Here are examples on how to issue date command with those options:

[email protected]:~$ date --rfc-3339=date
2009-07-02
[email protected]:~$ date --rfc-3339=seconds
2009-07-02 14:18:23+08:00
[email protected]:~$ date --rfc-3339=ns
2009-07-02 14:18:34.629526452+08:00

If you want to check what is the day and date 15 days ago when you take emergency leave, for example, you can issue date command with --date=stringoption. See example below:

[email protected]:~# date --date='15 days ago'
Wed Jun 17 04:09:12 MYT 2009

You can also use weeks, months and years with --date=string option:

[email protected]:~# date --date='15 years ago' +'I know what you did on %D'
I know what you did on 07/02/94
[email protected]:~# date --date='15 months ago' +'I also know what you did on %F'
I also know what you did on 2008-04-02
[email protected]:~# date --date='15 weeks ago' +'You did it again on %A %B %Y'
You did it again on Thursday March 2009

The Linux date example above shows three different formats of output. We can do that using `date' conversion specifiers. Please check date manual pages for more information.

Linux date command set date and time examples

We did mention earlier that Linux date command can be used to set date and time in Linux system. Here are some examples on how to change date and time also the format of the output.

Use date command to set date and time in 24 hours format (0-24 format):

[email protected]:~# date '-s 2009-07-02 15:43:13 +0800'
Thu Jul 2 15:43:13 MYT 2009

We can also change the way time displayed to 12 hours (1-12 format). See the date command example below:

[email protected]:~# date '-s 2009-07-02 03:48:13 +0800'
Thu Jul 2 03:48:13 MYT 2009

That's it.

Back to Linux basic commands main page.

Add new comment