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.

Share this page:

Suggested articles

3 Comment(s)

Add comment

Comments

From: Pete at: 2017-07-03 17:28:05

When I need to make a file with a YYYY-MM-DD portion in it:

date "+%F"

Makes sorting trivial.

From: Stephane at: 2017-07-04 09:08:36

Here a small bash function that I wrote to display the time in different timezones  It makes use of the date command.

Simply drop it in your .bashrc

Don't know how to make a proper code section in that comment so formating will probably be crap. Sorry

# tz    ## Display the specified time in multiple timezones (default is "now").## Use tzselect to find the proper timezone names (see ZONES below).   ## Timezones are sorted by their UTC offsets.## Timezones with the same UTC offset than the current timezone# are marked with '*'## However, they will be marked with '?' instead if the UTC offset # of the current time (now) and of the specified time are not equal # This usually indicates a summer/winter time change.   ## Examples:  (see 'info date' for more)##   tz                    : for current time (or "now")  #   tz 14:00              : for 14:00 today in the current timezone#   tz 14:00+4            : for 14:00 today in UTC+4 #   tz tomorrow 2pm PST   : for 14:00 tomorrow in Pacific Standard Time #   tz 2pm July 3 CEST    : for Friday July 3 14:00 2015 in Central European Summer Time ## Remark: Not all symbolic timezone names work as expected.#         First because some of them are only valid during parts of the year (e.g CET#         vs CEST in Central Europe) and also because some names are ambiguous.#         For instance, CST may refer to Central Standard Time in the US and #         to China Standard Time in China. #         Using UTC or GMT relative timezones is usually safer.## http://www.timeanddate.com/time/map/#tz () {    local A D T tz loc M UTC0 UTC1     local -A LOCATIONS    D="$*"    [ -n "$D" ] || D=now        T=$(date -d "$D" +%s) || return    # Add here your preferred locations and their timezone.    # See in tzselect or in /usr/share/zoneinfo/ for the values    LOCATIONS["Californie"]="US/Pacific"   # PST/PDT    LOCATIONS["Texas"]="US/Central"        # PST/PDT    LOCATIONS["New York"]="US/Eastern"     # EST/EDT    LOCATIONS["UK"]="Europe/London"        # GMT/BST    LOCATIONS["France"]="Europe/Paris"     # CET/CEST     LOCATIONS["Moscou"]="Europe/Moscow"    # MSK    LOCATIONS["Inde"]="Asia/Calcutta"      # IST (India Standard Time )     LOCATIONS["Japan"]="Asia/Tokio"        # JST     # Get current timezone now (in $UTC0) and at requested time (in $UTC1)    # They may be different because of summer/winter time changes.     UTC0=$(LC_ALL=C  date -d "now" +"%z")    UTC1=$(LC_ALL=C  date -d "@$T" +"%z")    for loc in "${!LOCATIONS[@]}" ; do        tz=${LOCATIONS[$loc]}        # echo "@ $loc tz"        # UTC2 is the UTC timezone of $tz at the given time         UTC2=$(LC_ALL=C TZ="$tz" date -d "@$T" +"%z")        if [ "$UTC1" == "$UTC2" ] ; then            if [ "$UTC0" == "$UTC1" ] ; then                M="*"  # This is consistent with current timezone.            else                M="?"  # Also but probably with a summer/winter time change.            fi        else            M=" "        fi        A=$(LC_ALL=C TZ="$tz" date -d "@$T" +"%a %b %d %H:%M (UTC%:z) %4Z $tz")        # Prefix each output line with its numeric timezone.         # That prefix will we removed after sorting.        printf "%s %c%-17s %s\n" "$UTC2" "$M" "$loc" "$A"     done  | sort -n | cut -c 7-}

From: Stephane at: 2017-07-04 09:39:26

What a mess :-)

For those that are interested, I dropped the file in http://www.chauveau-central.net/pub/tz.bash 

This is not a shell script. You will need to source it or to copy it into your .bashrc file before you can use the tz function.