date

systemlinux
The date command is one of the most frequently used commands in Linux/Unix-like operating systems. date The date command displays or sets the system date and time. It can format the output in various ways, convert between time zones, and perform date arithmetic.

Quick Reference

Command Name:

date

Category:

system

Platform:

linux

Basic Usage:

date +"%Y-%m-%d %H:%M:%S"

Common Use Cases

  • 1

    Time synchronization

    Display or set the current system date and time

  • 2

    Formatting timestamps

    Convert between different date and time formats

  • 3

    Script timestamping

    Add timestamps to logs or output in shell scripts

  • 4

    Date calculations

    Perform date arithmetic for scheduling and planning

Syntax

date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

Options

Option Description
-d, --date=STRING Display time described by STRING, not 'now'
-f, --file=DATEFILE Like --date; once for each line of DATEFILE
-I[FMT], --iso-8601[=FMT] Output date/time in ISO 8601 format. FMT='date' for date only, 'hours', 'minutes', 'seconds', or 'ns'
-R, --rfc-email Output date and time in RFC 5322 format (e.g., Thu, 21 Dec 2000 16:01:07 +0200)
-r, --reference=FILE Display the last modification time of FILE
-s, --set=STRING Set time described by STRING
-u, --utc, --universal Print or set Coordinated Universal Time (UTC)
--help Display help information and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the date command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

# Display current date and time
date
# Display date in a specific format date "+%Y-%m-%d %H:%M:%S"
# Display date in UTC time date -u
# Display specific time zone Advanced
TZ='America/New_York' date

Advanced Examples:

# Set system date and time (requires root privileges)
sudo date -s "2023-06-15 14:30:00"
# Set system date and time using MMDDhhmm[[CC]YY][.ss] format sudo date 061514302023.00 # Display the date 7 days ago date -d "7 days ago" # Display the date next Friday date -d "next Friday" # Convert a specific date to seconds since epoch date -d "2023-01-01" +%s # Convert seconds since epoch to date date -d @1672531200 # Display a different locale's date format LC_TIME=de_DE.UTF-8 date # Format date for log files date "+%Y%m%d-%H%M%S"

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Format Specifiers:

The date command supports many format specifiers for customizing output:

Format Description
%a Abbreviated weekday name (Sun..Sat)
%A Full weekday name (Sunday..Saturday)
%b Abbreviated month name (Jan..Dec)
%B Full month name (January..December)
%c Locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C Century (00-99)
%d Day of month (01-31)
%D Date in MM/DD/YY format
%H Hour (00-23)
%I Hour (01-12)
%j Day of year (001-366)
%m Month (01-12)
%M Minute (00-59)
%s Seconds since 1970-01-01 00:00:00 UTC
%S Second (00-60)
%T Time in 24-hour format (HH:MM:SS)
%u Day of week (1-7), 1 is Monday
%U Week number (00-53), with Sunday as first day of week
%w Day of week (0-6), 0 is Sunday
%W Week number (00-53), with Monday as first day of week
%y Last two digits of year (00-99)
%Y Full year (e.g., 2023)
%z +hhmm numeric time zone (e.g., -0400)
%Z Alphabetic time zone abbreviation (e.g., EDT)

Common Use Cases:

  • Creating timestamps for log files and backups
  • Calculating time differences
  • Converting between different date formats
  • Scheduling tasks based on specific dates
  • Setting system time for servers and devices
  • Working with time zones for global operations

Date Calculations:

The -d or --date option accepts a wide range of relative date specifications:

  • Time units: second(s), minute(s), hour(s), day(s), week(s), month(s), year(s)
  • Directions: ago, next, last, this, tomorrow, yesterday
  • Day names: Monday, Tuesday, etc.
  • Combinations: "next Monday", "2 days ago", "1 year 2 months 3 days ago"

Setting System Time:

To set the system time using date:

  • You must have administrative privileges (use sudo)
  • The format must be precise
  • Consider using Network Time Protocol (NTP) for production systems
  • Setting incorrect time can affect logs, certificates, and scheduled tasks

Tips & Tricks

1

Use the +format option to specify the output format

2

Use the -d string option to display the date for a specific string

3

Use the -s string option to set the system date and time

4

Use the -u option to display the Coordinated Universal Time (UTC)

5

Use the -R option to display the date in RFC 5322 format

Common Use Cases

Time synchronization

Display or set the current system date and time

Formatting timestamps

Convert between different date and time formats

Script timestamping

Add timestamps to logs or output in shell scripts

Date calculations

Perform date arithmetic for scheduling and planning

Time zone conversion

Display times adjusted for different time zones

Related Commands

These commands are frequently used alongside date or serve similar purposes:

Use Cases

1

Time synchronization

Display or set the current system date and time

2

Formatting timestamps

Convert between different date and time formats

3

Script timestamping

Add timestamps to logs or output in shell scripts

4

Date calculations

Perform date arithmetic for scheduling and planning

5

Time zone conversion

Display times adjusted for different time zones

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the date command works in different scenarios.

$ date
View All Commands