Linux date Command Examples

Filed Under: UNIX/Linux

Unix/Linux date command is a command used for displaying the system’s date and time. In addition, the command can also be used to modify or set the system’s date and time. For one to change the operating system’s time and date, one must be logged in as the root user. In this guide, we examine the Linux date command and how it is used.

Linux Date command with no option

When used as it is without any options, date command displays system’s date and time as shown:

$ date

Sample Output

Display System Time And Date Using Date Command

The above snippet shows the time in the EAT (East Africa Timezone) timezone (local system timezone).

Display system time in Greenwich Mean-time (UTC)

In our previous example, the system time was display in EAT timezone. To display time in UTC or Greenwich Mean Time, use the -u option

$ date  -u

Sample Output

Display System Time In UTC

Display both local and UTC time using timedatectl command

Another handy command when it comes to displaying system time is the timedatectl command. Using the command, you can display both local and UTC time as shown.

$ timedatectl

Sample Output

Timedatectl Command

Date with –date or -d option

The -d or --date option is used to convert date from numerical to string format. For example, to convert 3/12/2019 to a string, run

$ date --date=" 3/12/2019 "

Sample Output

Convert Date To String

Using –date option to display earlier dates

The --date option can also be used to display past dates. Simply pass a string in the arguments and the date will be converted in digital form.

For example, to display the date 10 days ago, use the syntax

date --date=" 10 day ago "

Sample Output

Display 10 Days Ago

To display the date 5 years ago run.

date --date=" 5 year ago "

Sample Output

Display 5 Years Ago

To display yesterday’s date run.

date --date=" yesterday "

Sample Output

Display Yesterdays Date

Let’s now see how you can display future dates.

Using –date option to display future dates

Displaying future dates is easy and is similar to the previous example. You simply need to define the string of the future date in the argument.

For instance, to display tomorrow’s date run.

date --date=" tomorrow "

Sample Output

Use Date To Display Tomorrows Date In Digits

To display next Friday’s date and time run:

date --date=" next fri "

Sample Output

Display Next Fridays Date

To display date and time after 2 years from the current date run:

date --date=" 2 year "

Sample Output

Display Date And Time After 2 Years

Setting System date and time

Apart from displaying system date and time, date command can be used to change the date and time of your Unix/Linux system. You can achieve this using the --set option as shown in the example below.

Syntax

$ date --set="date to be set"

For example,

$ date --set="Mon July 8 11:54:50 EAT 2019"

Sample Output

Set Date And Time Using Date Command

Display last modified timestamp of a file

To check the last time a file was modified, use the -r option as shown.

$ date -r

Sample Output

Check Timestamp Of When A File Was Last Modified

Format specifiers used with date command

Before we wrap up, here is a list of format options you can use to define the output of date command.


%D: Display date as mm/dd/yy.       
%d: Display the day of the month (01 to 31).       
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of the year (01 to 12).
%y: Displays the last two digits of the year(00 to 99).
%Y: Display four-digit year. 
%T: Display the time in 24-hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

The syntax of using these options is as follows.

$date "+%[format-option]"

For example to display date as mm/dd/yy run:

$date "+%D"

Sample Output

Date Format Specifier

To display the day of the week in a string format run:

$date "+%a"

Sample Output

Display The Date In A String Format Using Format Specifiers

And so on and so forth.

You have come to the end of this guide. We hope it has been helpful to you and we believe that you are now comfortable using the date command. Your feedback is welcome.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages