The date command in Linux is used for displaying, setting, or manipulating the system date and time. It also allows users to display time in various formats and calculates the past and future dates. In order to change the date and time you should need a user with root or sudo privileges.
Syntax
The basic syntax of date command:
date [option]... [+format]
Where format is a sequence of characters that specifies how the date and time are formatted.
Options
Useful options of date command:
-d
,--date=STRING
: Display time represented by the STRING.-f
,--file=DATEFILE
: Parse a file and display date and time.-I
,--iso-8601[=TIMESPEC]
: Display date and time in ISO 8601 format.-r
,--reference=FILE
: Display the last modification time of FILE.-R
,--rfc-2822
: Display date and time in RFC format.-s
,--set=STRING
: Set the time represented by STRING.-u
,--utc
,--universal
: Display in UTC time.
Basic Usage
By default date command displays the current date and time in the default format.
Command:
date
The default date format depends on the LC_TIME defined in locale. Locale files are typically stored in /usr/share/i18n/locales/
and directives d_t_fmt
, d_fmt
, and t_fmt
specify the formats for date and time, date only, and time only, respectively.
Date in Custom Formats
The displayed terminal output of the Linux date command can be formatted using format control characters which are preceded by a ‘+’ symbol. All these format controls proceed with the ‘%’ sign and are followed by their values.
Example:
date +"Year: %Y, Month: %m, Day: %d"
To display the date in the format of YYYY-MM-DD:
date +%Y-%m-%d
Formatting characters:
- %a - Prints weekday’s name in short format (e.g., Mon).
- %A - Used to display full weekday name (e.g., Monday).
- %b - Display the name of the month in short form (e.g., Jan).
- %B - Used to display the full month’s name (e.g., January).
- %d - Displays the month’s day (e.g., 05).
- %H - Display Hour (00..23).
- %I - Display Hour in (01..12) format.
- %j - Displays the Day of the year (001..366).
- %m - Displays Month in number (01..12).
- %M - Print Minutes in 00..59 sec.
- %S - Displays seconds (00..60).
- %u - Display week’s day in number (1..7).
- %Y - Used to display Full-year (e.g., 2019).
Examples
Let's look into some useful examples of date command
1. Getting the date of the day in future or past
To print tomorrow’s date, type the following command:
date -d "tomorrow"
Display the next Friday date:
date -d "next Friday"
To display the date and time from exactly one year ago from the current date:
date -date "1 year ago"
Display the yesterday date:
date -d "yesterday"
Similarly, to display the last Friday date:
date -d "last Friday"
2. Reading date from a string
Display the specific date and time "2020-10-09 10:22:47", rather than the current date and time.
date -d "2020-10-09 10:22:47"
Include customization to it:
date -d "12 Jan 2021" +"%A, %d %B %Y"
3. Show the last modification time of a file
To display the last modification date of the ‘/etc/hosts’ file:
date -r /etc/hosts
4. Using as an Epoch converter
To display the current date and time in seconds since the Unix Epoch (January 1, 1970 00:00:00 GMT):
date +%s
This displays the number of seconds since the Unix Epoch (00:00:00 UTC on 1 January 1970) for the date October 25, 1980.
date -d "1980-10-25" +"%s"
5. Set a date
To manually set the date and time to 2:30 PM, October 13, 2021:
sudo date --set="20211013 05:30"
Note: In most modern Linux distributions, the system time is usually synchronized with NTP, often through the systemd-timesyncd service or other similar services.
6. Display output in ISO 8601 format
To outputs the current date and time up to seconds precision, and includes the timezone information.
date --iso-8601=seconds
7. Display output in RFC 3339 format
This displays the current date and time up to the precision of seconds, formatted according to the Internet-standard RFC 3339:
date --rfc-3339=seconds
8. Using date with other commands
To create a backup of a MySQL database with a filename containing the current date:
mysqldump database_name > database_name-$(date +%Y%m%d).sql
Here the date command is used to assign the current date and time to a variable in a specific format:
$ date_current=$(date "+%F-%H-%M-%S")
$ echo $date_current
Comments