Skip to content

Latest commit

 

History

History
146 lines (102 loc) · 3.83 KB

date.md

File metadata and controls

146 lines (102 loc) · 3.83 KB

date

The date command is used to display and set the system date and time in Linux. It can format the date and time in various ways according to user requirements.

Basic Usage

  • Syntax:

    date [options] [+format]
  • Examples:

    date
    • Displays the current date and time in the default format.
    date "+%Y-%m-%d"
    • Displays the current date in YYYY-MM-DD format.
    date "+%A, %B %d, %Y"
    • Displays the current date in a more verbose format, such as "Saturday, August 11, 2024".
    date -d "next Monday"
    • Displays the date of the next Monday.
    date -u
    • Displays the current date and time in Coordinated Universal Time (UTC).

Common Options

  • +format:

    • Specifies the format for displaying the date and time. Format specifiers are replaced with the corresponding values.
    date "+%H:%M:%S"
    • Displays the current time in HH:MM:SS format.
  • -d [string] or --date=[string]:

    • Displays the date described by the provided string.
    date -d "2024-08-11"
    • Displays the date for August 11, 2024.
  • -u or --utc:

    • Displays the date and time in UTC instead of the local time.
    date -u
    • Displays the current UTC date and time.
  • -s [string] or --set=[string]:

    • Sets the system date and time to the specified value.
    sudo date -s "2024-08-11 12:00:00"
    • Sets the system date and time to August 11, 2024, 12:00 PM.
  • --help:

    • Displays help information about the date command.
    date --help
    • Shows usage information and available options.

Common Format Specifiers

Format Specifier Description
%Y Year (e.g., 2024)
%m Month (01-12)
%d Day of the month (01-31)
%H Hour (00-23)
%M Minute (00-59)
%S Second (00-59)
%A Day of the week (e.g., Monday)
%B Month name (e.g., August)
%T Time in HH:MM:SS format

Quick Tips

  • Custom Formats:

    • Use format specifiers to customize how date and time are displayed.
    date "+%Y-%m-%d %H:%M:%S"
    • Displays the date and time in YYYY-MM-DD HH:MM:SS format.
  • Relative Dates:

    • Use -d with relative dates to find future or past dates.
    date -d "3 days ago"
    • Displays the date 3 days before today.
  • Combining with Other Commands:

    • Use date in scripts or commands to include timestamps.
    echo "Backup completed at $(date)"
    • Prints a message with the current date and time.
  • Read this

root@host ~]# **`date +%F`** ![1](https://rha.ole.redhat.com/rol/static/roc/Common_Content/images/1.svg)
2022-03-10
[root@host ~]# **`date -d "+30 days" +%F`** ![2](https://rha.ole.redhat.com/rol/static/roc/Common_Content/images/2.svg)
2022-04-09
[root@host ~]# **`chage -E $(date -d "+30 days" +%F) cloudadmin10`** ![3](https://rha.ole.redhat.com/rol/static/roc/Common_Content/images/3.svg)
[root@host ~]# **`chage -l cloudadmin10 | grep "Account expires"`** ![4](https://rha.ole.redhat.com/rol/static/roc/Common_Content/images/4.svg)
Account expires						: Apr 09, 2022

Summary

The date command is a versatile tool for displaying and setting the system date and time in Linux. It supports a variety of formats and options to customize the output and can handle both absolute and relative date calculations. Understanding how to use date effectively can assist with time management and automation tasks.