LinuxCommandLibrary

date

Display current date and time

TLDR

Display the current date using the default locale's format

$ date +%c
copy

Display the current date in UTC, using the ISO 8601 format
$ date [[-u|--utc]] +%Y-%m-%dT%H:%M:%S%Z
copy

Display the current date as a Unix timestamp (seconds since the Unix epoch)
$ date +%s
copy

Convert a date specified as a Unix timestamp to the default format
$ date [[-d|--date]] @[1473305798]
copy

Convert a given date to the Unix timestamp format
$ date [[-d|--date]] "[2018-09-01 00:00]" +%s [[-u|--utc]]
copy

Display the current date using the RFC-3339 format (YYYY-MM-DD hh:mm:ss TZ)
$ date --rfc-3339 s
copy

Set the current date using the format MMDDhhmmYYYY.ss (YYYY and .ss are optional)
$ date [093023592021.59]
copy

Display the current ISO week number
$ date +%V
copy

SYNOPSIS

date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
date [-s|--set=STRING]
date [-r|--reference=FILE]
date [--date=STRING]

PARAMETERS

--date=STRING
    Display time described by STRING, not 'now'.

--set=STRING, -s
    Set the date and time described by STRING. Requires root privileges.

--utc, --universal, -u
    Print or set Coordinated Universal Time (UTC).

--iso-8601[=TIMESPEC]
    Output date/time in ISO 8601 format. TIMESPEC can be 'date', 'hours', 'minutes', 'seconds', 'ns'.

--rfc-3339[=TIMESPEC]
    Output date/time in RFC 3339 format. TIMESPEC similar to --iso-8601.

--reference=FILE, -r
    Display the last modification time of FILE.

--help
    Display help message and exit.

--version
    Output version information and exit.

+FORMAT
    Control the output format. FORMAT is a string with special characters like %Y for year, %m for month, etc.

DESCRIPTION

The date command in Linux is a versatile utility used to display or set the system date and time. When invoked without arguments, it prints the current date and time in a default format. It's widely used in shell scripts for generating timestamps, logging, and naming files uniquely based on the current time.

Users can customize the output format extensively using format control sequences. Additionally, date allows system administrators to adjust the system clock, either to a specific time or by parsing a human-readable date string. It interacts with the system's time zone settings and can display time in UTC. While date manages the system clock, hwclock is typically used to manage the hardware clock (RTC). Proper synchronization between the two is important for persistent time settings across reboots.

CAVEATS

date command operations on the system clock often require root privileges (e.g., using --set or supplying a new date/time string). Changes made by date are to the system clock, which is distinct from the hardware clock (RTC). To ensure time persistence across reboots, the hardware clock might need to be synchronized with the system clock using hwclock. Time zone handling relies on the TZ environment variable and /etc/localtime configuration; incorrect settings can lead to unexpected output.

COMMON FORMAT SPECIFIERS

The +FORMAT option uses various specifiers to control output:
%Y: 4-digit year (e.g., 2023)
%m: Month as a decimal number (01-12)
%d: Day of the month (01-31)
%H: Hour (00-23)
%M: Minute (00-59)
%S: Second (00-60)
%s: Seconds since 1970-01-01 00:00:00 UTC
%Z: Alphabetic time zone abbreviation (e.g., EST)
%A: Full weekday name (e.g., Monday)
%B: Full month name (e.g., January)
%c: Locale's appropriate date and time representation

HISTORY

The date command has been a fundamental part of Unix-like operating systems since their inception, making it one of the oldest and most consistently used utilities. It is part of the GNU Core Utilities (coreutils) package in Linux distributions, ensuring its widespread availability and standardization. Over time, it has evolved to support a richer set of formatting options, various input date string parsing capabilities, and features like ISO 8601 and RFC 3339 output, reflecting modern needs for precise and standardized time representation. Its core functionality for displaying and setting time remains vital for system administration, scripting, and general user queries.

SEE ALSO

hwclock(8), timedatectl(1), TZ(5), ntpdate(8), cron(8)

Copied to clipboard