Date Command - Format & Convert Dates with Linux/Unix Commands

Learn to format and convert dates using the

Date Command Examples

The date command is a powerful utility in Linux and Unix-like systems for displaying or setting the system date and time. It offers extensive formatting options and the ability to convert between different time representations, including Unix timestamps.

Common Date Formatting Specifiers

Here are some of the most frequently used format specifiers for the date command:

  • %a: Abbreviated weekday name (e.g., Sun, Mon)
  • %B: Full month name (e.g., January, February)
  • %F: Full date in ISO 8601 format (YYYY-MM-DD)
  • %T: Time in 24-hour format (HH:MM:SS)
  • %U: Week number of the year (Sunday as the first day of the week, 00-53)
  • %W: Week number of the year (Monday as the first day of the week, 00-53)
  • %X: Time in a locale-specific format
  • %Y: Four-digit year (e.g., 2023)
  • %Z: Timezone name (e.g., UTC, EST)

Advanced Date Command Usage

Beyond basic formatting, the date command can handle more complex tasks like creating file-friendly timestamps and converting Unix timestamps:

  • File-friendly timestamp: date +"%Y%m%d_%H%M%S" creates a string like 20231027_103055, perfect for naming log files or backups.
  • Convert Unix timestamp to Date (Linux): date -d @1440359821. This command takes a Unix timestamp (seconds since the epoch) and converts it into a human-readable date and time.
  • Convert Unix timestamp to Date (macOS/BSD): date -r 1440359821. Similar to the Linux version, but uses the -r flag.

Working with Timezones

The date command allows you to display times in different timezones:

  • Show current timezone: date +%Z displays the system's current timezone abbreviation.
  • Show date in UTC/GMT: date -u displays the date and time in Coordinated Universal Time.
  • Show date in a specific timezone: TZ='CET' date or TZ='America/Los_Angeles' date allows you to specify a timezone using the TZ environment variable. For a comprehensive list of timezone names, you can use the tzselect command.
  • RFC format with TZ offset: date -R displays the date in RFC 5322 format, including the timezone offset.

For more detailed information on the date command and its extensive options, refer to the official Linux man page.

Understanding and utilizing the date command effectively is crucial for system administration, scripting, and development tasks that involve time-based operations.

---
tags: [ date, time ]
---
# To print Abbreviated weekday name:
date +"%a"

# To print Full month name:
date +"%B"

# To print ISO date (same as %Y-%m-%d):
date +"%F"

# To print Time (same as %H:%M:%S):
date +"%T"

# To print Sunday week number (00 to 53):
date +"%U"

# To print Monday week number (00 to 53):
date +"%W"

# To print Time (localized):
date +"%X"

# To print 4-digit year:
date +"%Y"

# To print Timezone name:
date +"%Z"
# To print the date in a format suitable for affixing to file names:
date +"%Y%m%d_%H%M%S"

# To convert a Unix timestamp to Date (Linux):
date -d @1440359821

# To convert a Unix timestamp to Date (OSX):
date -r 1440359821

# To show the current timezone:
date +%Z

# To show date in RFC format with TZ offset:
date -R

# To show date in UTC/GMT:
date -u

# To show date in CET:
TZ=CET date

# To show the time on the west coast of the US (use tzselect(1) to find TZ):
TZ='America/Los_Angeles' date