Lesson 68: date Command
In this lesson, you'll learn how to use the date command to display, set, and format system date and time in Linux.
The date command is an external bash program that allows you to set or display the system date and time.
It also provides several formatting options. It is installed in all Linux distros by default.
$ which date
$ type -a date
/usr/bin/date
date is /usr/bin/date
Type the date command in the terminal to display the current date and time.
$ date
Wed Nov 11 13:06:59 IST 2023
date Command Syntax
$ date [OPTIONS] [+FORMAT]
Date Format Specifiers
| Specifier | Description |
|---|---|
%Y |
Full 4-digit year (e.g. 2023) |
%y |
Last 2 digits of the year (e.g. 23) |
%m |
Month as a number (01-12) |
%b |
Abbreviated month name (e.g. Nov) |
%B |
Full month name (e.g. November) |
%d |
Day of the month (01-31) |
%A |
Full weekday name (e.g. Wednesday) |
%a |
Abbreviated weekday name (e.g. Wed) |
%D |
Date in %m/%d/%y format |
%F |
Date in %Y-%m-%d format |
%H |
Hour in 24-hour format (00-23) |
%M |
Minutes (00-59) |
%S |
Seconds (00-60) |
%N |
Nanoseconds |
%s |
Unix epoch time (seconds since Jan 1, 1970) |
%T |
Time in HH:MM:SS format (24-hour) |
%r |
Time in 12-hour format |
1. Change Linux System Date and Time
Using the date command, system date, time, and timezone can be modified. The change has to be synced with the hardware clock.
$ date --set="Thu Nov 12 13:06:59 IST 2020"
$ hwclock --systohc
Thu Nov 12 13:06:59 IST 2020
2. Formatting Options
A good place to get the full list of formatting options is the man page.
$ man date
Key points about formatting:
- To apply formatting, use
+followed by%formatter. - To get a list of formatting options for GNU/Linux, refer to the man page.
- To get a list of formatting options for BSD, refer to the BSD man page.
The two important parts of the date command are using Format +% and the "--date" option.
To apply formatting, add the plus sign (+) followed by %formatter as shown in the examples below.