Linux
The date
command in Linux is a versatile tool used for displaying and setting the date and time. It also allows users to perform various operations related to date and time calculations.
Syntax:
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Example:
date -d "next Monday"
--debug
: Annotate the parsed date and provide a warning about controversial usage to stderr.
-f
or --file=DATEFILE
: Similar to the ‘–date’ option.
-I[FMT]
or --iso-8601[=FMT]
: Display the date/time in ISO 8601 format.
Example:
date -Iseconds
date -r filename
Date command Formatting Options
Here are some common formatting options that can be used with the +FORMAT
argument:
%a
: Abbreviated weekday name (e.g., Sun)%A
: Full weekday name (e.g., Sunday)%b
: Abbreviated month name (e.g., Jan)%B
: Full month name (e.g., January)%c
: Date and time (e.g., Thu Mar 3 23:05:25 2005)%C
: Century (e.g., 20)%d
: Day of the month (e.g., 01)%D
: Date (e.g., %m/%d/%y)%e
: Day of the month, space-padded (e.g., %_d)%F
: Full date (e.g., %Y-%m-%d)%g
: Last two digits of the year of ISO week number (see %G)%G
: Year of ISO week number (see %V)%h
: Abbreviated month name (similar to %b)- … and many more.
Examples of date command in linux
Basic Usage:
date
Custom Formatting:
date +'%A, %d %B %Y %T'
Displaying Previous Modification Time of a File:
date -r filename
Setting System Date and Time:
date --set="202012312359"
Displaying Epoch Time:
date +%s
Displaying ISO 8601 Date/Time:
date -Iseconds
Calculating Date (e.g., Next Monday):
date -d "next Monday"
Displaying RFC 3339 Date/Time:
date --rfc-3339=seconds
Setting Timezone:
TZ='India' date
Setting the system date and time using date
directly is not recommended in production environments. The Linux system clock is usually synchronized using services like systemd-timesyncd
or ntp
.
The date
command is a powerful tool for manipulating and displaying date and time information on a Linux system. It supports various formatting options, making it flexible for different use cases.