0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use Date Command in Linux: Syntax and Examples

The date command in Linux shows the current date and time or formats a specified date/time. You can customize output using format options like +%Y-%m-%d to display the date as year-month-day.
📐

Syntax

The basic syntax of the date command is:

  • date: Displays the current date and time.
  • date [OPTION]... [+FORMAT]: Shows date/time formatted according to FORMAT.
  • date -d 'STRING': Displays the date/time described by STRING instead of now.

Common format specifiers include:

  • %Y: Year (4 digits)
  • %m: Month (01-12)
  • %d: Day of month (01-31)
  • %H: Hour (00-23)
  • %M: Minute (00-59)
  • %S: Second (00-59)
bash
date [+FORMAT]
date -d 'STRING'
💻

Example

This example shows how to display the current date in year-month-day format and how to show the date for a specific day.

bash
date +"%Y-%m-%d"
date -d "2024-01-01" +"%A, %B %d, %Y"
Output
2024-06-15 Monday, January 01, 2024
⚠️

Common Pitfalls

Common mistakes include:

  • Forgetting the + before the format string, which causes the format to be ignored.
  • Using incorrect format specifiers that produce unexpected output.
  • Not quoting the date string with -d, which can cause parsing errors.
bash
date "%Y-%m-%d"  # Wrong: missing + before format

# Correct usage:
date +"%Y-%m-%d"

# Wrong usage without quotes for -d:
date -d 2024-01-01

# Correct usage with quotes:
date -d "2024-01-01"
📊

Quick Reference

Option/FormatDescriptionExample Output
dateShow current date and timeSat Jun 15 14:30:00 UTC 2024
date +"%Y-%m-%d"Show date as year-month-day2024-06-15
date -d "tomorrow"Show date for tomorrowSun Jun 16 14:30:00 UTC 2024
date +"%H:%M:%S"Show current time in 24-hour format14:30:00
date -d "2024-01-01" +"%A"Show day of week for a dateMonday

Key Takeaways

Always use a plus sign (+) before format strings to customize output.
Use -d with quotes to specify a different date or time to display.
Common format codes like %Y, %m, and %d help format dates clearly.
The date command can show current or any specified date/time easily.
Quoting strings and formats avoids parsing errors and unexpected results.