How To Use DATE Command in Linux

How to use the date command in Linux and what are the different utilities that it offers.

When using Linux distributions we have hundreds of commands at our disposal that help us obtain accurate information about services, processes, system components and many other things, all focused on proper administrative and support management.

One of these commands is the date which is used to obtain the current date and time of the selected system:

 

But more than this, this command gives us the possibility to see or calculate a date in a format that we specify or the root user can use it to configure the operating system clock if necessary.

DATE Command Basics

We can see in the image above that when you execute only the date command without any additional parameter, it will display the current date and time of the system, including the day of the week, the month, the time, the time zone and the year.

If we wish to display the results on a specific date, it will be necessary to use the parameter -from entering the respective date:

date -d "2018-01-15"

This date command has multiple options at the display format level. For example, it will be possible to indicate the date with the format string by preceding it with a plus sign in the following way:

date +"Week number: %V Year: %y"

This will result in the current week number of the current year since% V is the format option to display the current week number, and% represents the last two digits of the year.

DATE: Format Options

Some of the most common formatting options when using date are:

Refers to the abbreviated name of the week

%a

Indicate the full name of the week

%A

Short name of the month

%b

Full name of the month

%B

Display the local date and time

%c

Display the day of the month

%d

It shows us the day of the current year

%j

Reflects the full date

%F

Indicate the elapsed minutes

%M

It allows us to see the nanoseconds passed

%N

Display time in 24-hour format

%R

Show the number of the week by taking Monday as the first day of the week

%W

Show the number of the week by taking Sunday as the first day of the week

%U

To see the different options we can execute the following command:

date --help

DATE: Modify Current Time Zone

By default, the date command uses the time zone defined in the /etc/localtime directory.

The environment variable TZ (TimeZone) can be used to override this behavior, we can check the TZ using the following command:

TZ=GMT date

Some examples of how we can use the date command defining certain times are:

Now

date -d now

Today

date -d today

Yesterday

date -d yesterday

Tomorrow

date -d tomorrow

Next Sunday

date -d sunday

Last Sunday

date -d last-sunday

 

DATE: Seconds

Using the date command allows us to perform another type of analysis, for example, we can take the date command to convert a date or time at the time of the Unix time (seconds since 00:00:00, January 1, 1970 ) and vice versa, in this case, we will execute the following with which we will see the seconds from epoch to the current time:

date +%s

The result will be the following:

Using this same format we can obtain the seconds of a certain date, for example:

date -d "2017-12-31" +"%s"

Convert Epoch to Linux DATE

Remember that epoch is the UNIX time format.

With date it is possible to convert epoch on a specific date by using one of the following commands:

date -d "UTC 2000-01-15 123456897 secs"
date -d @123456897

Also, we have the possibility of knowing the exact date of some date with the following line:

date -d "2000-01-15" +"%A"

Set a date manually and use the date command in Scripts and commands. At the time it is necessary to set a date or time different from the current one, simply execute the following syntax:

date --set="AAAAMMDD HH:SS"

It is possible to assign the result of the date command to a shell variable and then use it later in various scripts, for example, we can execute the following:

STARTTIME=`date`
echo $STARTTIME
sleep 7
echo $STARTTIME

We can see how the date command allows us to perform actions far beyond simply seeing the current date and time.

Similar Posts

Leave a Reply

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