When we work in the Linux command-line or write shell scripts, we often need to handle date and time values, such as outputting a date in our required formats, getting a relative date-time, or changing the system date and time.
In this tutorial, we’ll take a closer look at the date utility and learn its common usages.
2. Introduction to the date Command
The date command is preinstalled by default in all Linux distros since it’s a part of the GNU coreutils package.
The syntax of using the date command is:
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
If we don’t give any options, the date command will print the current system date and time including the day of the week, month, time, year and timezone:
$ date Sat 21 Mar 2020 08:51:23 PM CET
However, if we pass different options, the date command can do more than that.
Some common usages of the date command are:
- display the current date and time from a given timezone
- display the date and time in a required format
- display the date and time from a given string
- get a relative date and time
- set the system date and time
Let’s go through some examples to learn how to handle each of these.
3. Override the Timezone
3.1. Display Date and Time in UTC
The date command will output the date and time in the timezone defined in our system.
UTC is a special timezone. The date command supports the option -u to display the date and time in UTC:
$ date -u Sat 21 Mar 2020 08:51:03 PM UTC
3.2. Display Date and Time in Another Timezone
The date command will by default use the timezone defined by /etc/localtime. The /etc/localtime file is usually a symbolic link, pointing to a timezone file:
$ readlink /etc/localtime /usr/share/zoneinfo/Europe/Berlin
Sometimes, we want to know the date and time in another timezone. We can set the environment variable TZ to override the timezone setting for the date command:
$ date Sat 21 Mar 2020 10:07:37 PM CET $ TZ="EST" date Sat 21 Mar 2020 04:07:42 PM EST
$ TZ="Asia/Tokyo" date Sun 22 Mar 2020 06:07:55 AM JST
The changed environment variable TZ is only for the following date command, the system timezone retains the original value:
$ date Sat 21 Mar 2020 10:09:10 PM CET
4. Display Date and Time in Various Formats
The date command supports many display formatting options. We can pass a required format to date:
Let’s see a couple of examples of changing the date format:
$ date '+Current Date: %F%nCurrent Time: %H:%M:%S' Current Date: 2020-03-21 Current Time: 22:23:03 $ date '+Today is %A, the %wth day in the week %W.%nUnix timestamp for now: %s' Today is Saturday, the 6th day in the week 11. Unix timestamp for now: 1584826306
Some commonly used formats are:
- %A – locale’s full weekday name (for example, Sunday)
- %D – date; same as %m/%d/%y
- %F – full date; like %+4Y-%m-%d
- %s – seconds since 1970-01-01 00:00:00 UTC
- %y – last two digits of the year (00..99)
- %Y – year (for example, 2020)
- %Z – alphabetic time zone abbreviation (for example, EST)
We can find the full list of supported format sequences in the man page or the output of date –help.
5. Display Date From a Given String
We can pass a static date or time string value together with the -d option to the date command and let it parse and print the date and time in our required format.
For example, we can pass a date string to indicate 18 Nov 1976 18:18:18 to the date command in several ways:
$ date -d'1976-11-18 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'11/18/1976 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'18 Nov 1976 18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'19761118T18:18:18' Thu 18 Nov 1976 06:18:18 PM CET $ date -d'@217185498' Thu 18 Nov 1976 06:18:18 PM CET
From the examples above, we see that the date string pattern supported by the date command is pretty flexible. It can be a Unix timestamp following a @, an ISO date format, or even a human-readable date string.
6. Get Relative Date and Time
In the previous section, we’ve seen how flexible the date string for the -d option can be. In this section, we’ll continue using -d’dateString’ to let the date command calculate a relative date and time in the past or future.
In this section, all examples of relative date calculation will be based on the current date and time:
$ date Sun 22 Mar 2020 11:27:41 PM CET
6.1. yesterday and tomorrow
Let’s start with getting one day before and after the current date. It is pretty straightforward, we just pass yesterday and tomorrow with the -d option:
$ date -d'yesterday' Sat 21 Mar 2020 11:30:32 PM CET $ date -d'tomorrow' Mon 23 Mar 2020 11:30:37 PM CET
6.2. next, ago and last
Passing yesterday or tomorrow with the -d option is helpful for us to get one single day before and after the base date.
But what if we want to get an arbitrary number of days before and after? Or even in other units, such as weeks, months, years and so on?
The next, ago, and last expressions can help us with that:
$ date -d'3 days ago' Thu 19 Mar 2020 11:40:14 PM CET $ date -d'next 2 month' Thu 23 May 2020 12:40:23 AM CET $ date -d'last week' Sun 15 Mar 2020 11:40:29 PM CET $ date -d'next wed' Wed 25 Mar 2020 12:00:00 AM CET $ date -d'last Sunday' Sun 15 Mar 2020 12:00:00 AM CET
6.3. +x and -x
In addition to those human-readable date strings such as next and last, we can also pass a number with a unit to the -d option. A positive number will get a date-time in the future, while, a negative number indicates a date-time in the past:
$ date -d'+7 day' Mon 30 Mar 2020 12:54:43 AM CET $ date -d'-3 year' Wed 22 Mar 2017 11:54:51 PM CET $ date -d'10 week' Mon 01 Jun 2020 12:55:08 AM CET
This way makes it easy to calculate relative date-time programmatically, for example, in a shell script.
6.4. Get Relative Date and Time Based on a Given Date
So far, we’ve seen some examples of getting a relative date and time based on the current date-time. They’ll work for a given date string as well.
Let’s see some examples of getting relative date-time based on 1976-11-18:
$ date -d'19761118 -3 days' Mon 15 Nov 1976 12:00:00 AM CET $ date -d'19761118 last year' Tue 18 Nov 1975 12:00:00 AM CET $ date -d'19761118 tomorrow' Fri 19 Nov 1976 12:00:00 AM CET $ date -d'19761118 2 month ago' Sat 18 Sep 1976 12:00:00 AM CET
7. Change the System Time
We can use the date command with the -s option to change the system clock manually. Since changing the system time will affect current running programs, only the root user is allowed to do that change.
For example, the next command will set our system time to 1976-11-18 18:18:18 :
root# date -s "1976-11-18 18:18:18"
The date command is a very convenient utility to handle date and time values.
In this article, we’ve focused on and explored its common, practical usages.