Linux Date Command Tutorial for Beginners (8 Examples)

 

While working on the Linux command line, you might find yourself in situations where-in you need to display (or even change) the current system time. Not only that, if you work in a team with members in different timezones, you may want to keep yourself updated with time-related information for zones in which other members are sitting.

If you’re looking for a tool that lets you do all this (and much more), you’ll be glad to know there exists a command – dubbed date – that does all this. In this tutorial, we will discuss the basics of the ‘date’ command as well as how you can use it. But before we do that, it’s worth mentioning that all commands and instructions mentioned here have been tested on Ubuntu 16.04 LTS.

Linux date command

Here’s the generic syntax of the date command:

date [OPTION]… [+FORMAT]

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

date - print or set the system date and time
Display the current time in the given FORMAT, or set the system date.

The following Q&A-style examples should give you a good idea of how this command works.

Q1. How to get system date/time info using date command?

That’s the tool’s default behavior. To know your system’s date and time, all you have to do is to run the tool in the following way (yeah, sans any option):

date

Here’s the command in action:

How to get system date/time info using date command

So you can see that information like day, date, time, time zone, as well as year was displayed in output.

Q2. How to get date corresponding to a day?

More often than not, we look up to calendars to know what date it is on a particular day of the week. For example, the requirement could be to know the date for ‘next Tuesday’. You’ll be glad to know that this is also possible using the ‘date’ command.

The -d or –date command line option would be of help in this case:

date -d “next Tuesday”

Here’s the above command in action:

How to get date corresponding to a day

So, as you can see, the command revealed that next Tuesday is July 4.

The input to the -d/–date option can be of different types. Here’s how the man page describes it:

The --date=STRING is a mostly free format human readable date string such as 
"Sun,  29  Feb  2004  16:21:42  -0800"  or  "2004-02-29 16:21:42"  or  even "next Thursday".  
A date string may contain items indicating calendar date, time of day, time zone, day of week,
relative time, relative date, and numbers.  An empty string indicates the beginning of the day.
The date string format is more complex than is easily documented here but is fully described in 
the info documentation.

To access info documentation for date, use the following command:

info date

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

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

–iso-8601[=FMT]

Here’s how the man page explains ‘format’:

FMT='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to
the indicated precision. Example: 2006-08-14T02:34:56-0600

For example, I tested the following command:

date –iso-8601=seconds

And here’s the output it produced:

2017-06-27T14:20:39+05:30

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

As you’d have guessed, there’s a dedicated command line option for this as well: –rfc-3339. Like the option discussed in the previous section, this one also requires you to enter a format specifier.

The following screenshot shows this option in action:

How to display date/time in RFC 3339 format

Similarly, you can use the –rfc-2822 option to display output in that format.

Q5. How to use date to display last modification time of a file?

You can also use the date command to display the last modification time of a file. The tool’s -r option lets you do this. Here’s an example:

date -r file1

How to use date to display last modification time of a file

Q6. How to set system date/time using date command?

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

date -s STRING

NOTE: The available options that you can use as STRINGS are already explained in Q2 above.

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

date -s “2017-06-27 14:53:00”

Please note that you may have to use ‘sudo’ for the -s option to work. For those who aren’t sure what sudo is, head here.

Q7. How to display current time of some other location?

To make the ‘date’ command display current time of some other location – say, Melbourne in Australia – use it in the following way:

TZ=”Australia/Melbourne” date

Here’s the above command in action:

How to display current time of some other location

Note: You can use the ‘tzselect’ command to find the value you need to pass to TZ.

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

For this, use the -u command line option. For example, the following command will display the information in UTC:

date -u

How to print or set Coordinated Universal Time

Conclusion

As most of you’d agree, the date command isn’t at all difficult to understand and use. Plus, the fact that it can also be used while dealing with multiple geographic locations makes it an important command line tool. We’ve discussed most of the tool’s command line options here, so just try them out on your system. To know more, head to the command’s man page.

Source

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.