Javatpoint Logo
Javatpoint Logo

Linux date Command

Linux date command is used to display date, time, time zone, etc. It is also used to set the date and time of the Linux system. Generally, it is used to display the date in different formats and calculate dates over time.

Syntax:

Options

The following are some useful command line options of the date command:

-d, --date=STRING: It is used to display time described by STRING.

--debug: It is used to annotate the parsed date, and provide a warning about controversial usage to the stderr.

-f, --file=DATEFILE: It is similar to the '--date' option.

-I[FMT], --iso-8601[=FMT]: It is used to display the date/time in ISO 8601 format.

-R, --rfc-email: It is used to display the date and time in RFC 5322 format. For example, Mon, 14 Aug 2006 02:34:56 -0600.

--rfc-3339=FMT: It is used to display date/time in RFC 3339 format.

-r, --reference=FILE: It is used to display the previous modification time of the FILE.

-s, --set=STRING: It is used to set time described by STRING.

-u, --utc, --universal: It is used to display or set the UTC.

--help: It is used to display the help manual.

--version: It displays the version information.

Formatting options

%a: It is used to display the abbreviated weekday name (e.g., Sun)

%A: It is used to display the full weekday name (e.g., Sunday)

%b: it is used to display the abbreviated month name (e.g., Jan)

%B: It is used to display the full month name (e.g., January)

%c: It is used to display the date and time (e.g., Thu Mar 3 23:05:25 2005)

%C: It is used to display the century; like %Y, except omit last two digits (e.g., 20)

%d: It is used to display the day of the month (e.g., 01)

%D: It is used to display date; same as %m/%d/%y

%e: It is used to display the day of the month, space padded; same as %_d

%F: It is used to display the full date; same as %Y-%m-%d

%g: It is used to display the last two digits of the year of ISO week number (see %G)

%G: It is used to display the year of ISO week number (see %V); normally useful only with %V.

%h: It is similar to the '%b' option.

%H: It is used for an hour (00..23)

%I: It is used for an hour (01..12)

%j: It is used for the day of the year (001..366)

%k: It is used for the hour, space padded ( 0..23); same as %_H

%l: It is used for the hour, space padded ( 1..12); same as %_I

%m: It is used for the month (01..12)

%M: It is used for the minute (00..59)

%n: It is used for a new line

%N: it is used for nanoseconds (000000000..999999999)

%p: It is used to pass the equivalent of either AM or PM; blank if not known

%P: It is similar to the '%p' option, But, it is passed in lower case.

%q: It is used to display the quarter of a year (1..4)

%r: It is used to display the 12-hour clock time (e.g., 11:11:04 PM)

%R: It is used to display the 24-hour clock time in an hour and minute; same as %H:%M

%s: it is used to display the seconds since 1970-01-01 00:00:00 UTC

%S: it is used to display the second (00..60)

%t: It is used for a tab option.

%T: it is used to display the time; same as %H:%M:%S

%u: It is used for the day of the week (1..7); 1 is Monday

%U: It is used for a week number of year, it counts Sunday as the first day of the week (00..53)

%V: It is used for the ISO week number, it counts Monday as the first day of the week (01..53)

%w: It is used for the day of the week (0..6); 0 is Sunday

%W: It is used for the week number of year, It counts Monday as the first day of the week (00..53)

%x: It is used for the date representation (e.g., 12/31/99)

%X: It is used for the time representation (e.g., 23:13:48)

%y: It is used for the last two digits of the year (00..99)

%Y: It is used for a year

%z: It is used for the +hh:mm numeric time zone (e.g., -0400)

%:z: It is used for the +hh:mm numeric time zone (e.g., -04:00)

%::z: It is used for the +hh:mm:ss numeric time zone (e.g., -04:00:00)

%:::z: It is used for the numeric time zone with ':' to necessary precision (e.g., -04, +05:30)

%Z: It is used for the alphabetic time zone abbreviation (e.g., EDT)

Examples of the date Command

Let' see the following examples of the date command:

How to use the date Command

The date command is basic utility, and it can be used by executing without any argument. It will display the current date and time. Consider the below command:

The above command will display the current date and time of the system. Consider the below output:

Linux Date

Date Formatting

We can display the date in the format of our choice. There are several options to display the date in a specific format. Different date formats can be seen from the above list of formatting options. Consider the below commands:

From the above commands, the date will display in different formats. Consider the below output:

Linux Date

Similar as the above commands, there are other options available. See the complete list from the above formatting options.

Date Strings

We can specify the date in different date strings. The '-d' option is used to operate on a specific date. Consider the below command:

From the above command, we have a specified date in a different format. It will display the description of the specific date and time. Consider the below output:

Linux Date

We can also use the custom formatting as follows:

The above command will display the date in the given format. Consider the below output:

Linux Date

Also, we can pass the strings like "yesterday", "monday", "last monday" "next monday", "next month", "next year," and many more.

Consider the below commands:

The above commands will display the dates accordingly. Consider the below output:

Linux Date

Overriding the Timezone

By default, the date command displays the time according to the current time zone. But, we can change it by setting the environment variable 'TZ.' The default value of the 'TZ' variable is stored in the file "/etc/localtime". After changing the value of the environment variable, we can display the time from different timezones. Consider the below commands:

The above commands will display the time according to given timezones. Consider the below output:

Linux Date

Date Command as Epoch Converter

The date command can also be used to display the Epoch time. The Epoch time (Unix time or Unix timestamp or POSIX time) is the time (in seconds) that have been elapsed since January 1, 1970.

To display the time from the epoch to the current date, execute the command with the '%s' option as follows:

The above command will display the elapsed time from the epoch time. Consider the below output:

Linux Date

To convert the time as the current date, prefix the seconds with '@'. Consider the below command:

The above command will convert the time (in seconds) to the human-readable format. Consider the below output:

Linux Date

Display the last modification time of a file

The '-r' option is used to display the last modification time of the file. For example, to display the last modification time of the file 'Demo1.txt' execute the command as follows:

The above command will display the time when the given file is modified. Consider the below output:

Linux Date

Set the system date and time

We can set the time and date of the system by using the date command. However, it is not recommended to set the date and time of the system by the date command. Because The Linux system clock is synchronized using the sytemd-timesyncd or ntp services.

To set the time by the date command, use the "--set= time" option. For example, if we want to set the date and time as "08:15 pm, June 06, 2020", execute the command as follows:

The above command will set the time as specified. Consider the below output:

Linux Date
Next TopicLinux Cal




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA