8 Linux Date Command Examples for Beginners

While working on the Linux command line, you might get into a situation where you need to display (or even change) the current system time. Not only that, if you are working with team members who are in different time zones, you may want to update over time the information regarding the zone in which other members sit.

If you’re looking for a tool that does all of this (and more), you’ll be glad to know that there is a team date… In this article we will discuss the basic “date” commands as well as how you can use it. But before we do that, it’s worth noting that all the commands and instructions mentioned here have been tested on Ubuntu 16.04 LTS.

Linux date command

Here is the general syntax for the date command:

date [OPTION]... [+FORMAT]

And here’s what the man page says about it:

date - вывод или установка даты и время системы 

Отображение текущего времени в данном формате, или установка системной даты.

The following examples should give you a good idea of ​​how this command works.

Q1. How do I get the system date / time using the date command?

This is the default behavior. To find out the date and time of your system, all you have to do is run the program like this (yes, any option without):

date

Here’s the command in action:

date
Sun Jul  9 19:41:10 MSK 2017

So you can see that information like day, date, time, time zone, and also year was shown in the output.

Q2. How do I get the date corresponding to the day?

More often than not, we look at calendars to know the date on a specific day of the week. For example, the requirement might be to find out the date “when is next Tuesday.” You will be glad to know that this is possible with the “date” command.

-d or –Date the command line parameter will be of help in this case:

date -d "next Tuesday"

Here’s the above command in action:

date -d "next Tuesday"
Tue Jul 11 00:00:00 MSK 2017

So, as you can see, the team showed that the next Tuesday is July 4th.

The -d / -date input parameter can be of different types. This is how man describes this command:

--date = строка в основном свободный формат удобочитаемой строки даты, такие как 
«Sun, 09 Feb 2017 19:21:20 -1905» или «2017-02-09 19:21:20» или даже «в следующий четверг»,  

Строка даты может содержать элементы, указывающие на календарную дату, время суток, часовой пояс, день недели, 
относительное время, относительную дату и номер. Пустая строка указывает на начало дня. 

Формат строки даты является более сложным, чем описываемая здесь, но полностью описаны в 
информационной документации.

To access information in the documentation for a date, use the following command:

info date

Q3. How to display date / time information in ISO 8601 format?

In case you want a tool to display date / time information in ISO 8601 format, you can use the parameter –Iso-8601 command line. This option requires you to specify a format.

--iso-8601[=FMT]

This is how the man page explains “format”:

FMT='date' только для даты (по умолчанию), 'hours', 'minutes', 'seconds', или 'ns' для даты и времени с 
указанной точностью. Пример: 2017-07-09T19:20:30-0600

For example, we tested the following command:

date --iso-8601=seconds

The output produced:

2017-07-09T19:48:20+03:00

Q4. How to display date / time in RFC 3339 format?

As you might have guessed, there is a special command line parameter for this: –Rfc-3339… Alternatively, discussed in the previous section, one also requires you to enter a format.

The following example shows this option in action:

date --rfc-3339=seconds
2017-07-09 19:49:58+03:00

Alternatively, you can use –Rfc-2822 to be able to output data in this format.

Q5. How do I use the date to display the time the file was last modified?

You can also use the date command to display the time the file was last modified. Option -r allows you to do this. Here’s an example:

date -r file1

Conclusion:

date -r file1
Tue Jul 09 19:53:10 MSK 2017

Q6. How can I set the system date / time using the date command?

To set the system date / time to a different value, use the parameter -s command line. This option requires a string to be used as input to set the system time / date.

date -s STRING

Note: The available options that can be used as strings are already explained in Q2 above.

Here’s an example of how we used the -s option to set the date and time on our system:

date -s "2017-06-27 14:53:00"

Please note that you may need to use “sudo” for the -s option.

Q7. How can I display the current time elsewhere?

In order for the date command to display the current time elsewhere – say, Melbourne in Australia – use it as follows:

TZ="Australia/Melbourne" date

Here’s the above command in action:

TZ="Australia/Melbourne" date
Mon Jul 10 02:59:50 AEST 2017

Note: You can use the “tzselect” command to find the value you want for TZ.

Q8. How to print or set Coordinated Universal Time (UTC)?

To do this, use the parameter -u command line. For example, the following command will display information in UTC format:

date -u

# date -u
Sun Jul  9 16:32:28 UTC 2017

Conclusion

As most of you agree, the date command is not difficult to understand and use. In addition, the fact that it can also be used when working with multiple geographic locations makes it an essential command line tool. We’ve covered most of the command line options here, so just try them out on your system.

Sidebar