How to format date in Linux?

0236

In Linux, you can format the date using the date command with various format specifiers. Here’s how to do it:

Basic Usage

  1. Display Current Date and Time:
    date

Custom Formatting

To format the date output, use the + option followed by the desired format string:

  1. Common Format Examples:
  • YYYY-MM-DD Format:

    date '+%Y-%m-%d'
  • HH:MM:SS Format:

    date '+%H:%M:%S'
  • Full Date with Day and Month Names:

    date '+%A, %B %d, %Y'

    This will output something like: "Wednesday, March 31, 2021".

  • ISO 8601 Format:

    date '+%Y-%m-%dT%H:%M:%S'
  • Compact Date Format:

    date '+%y%m%d'

Common Format Specifiers

  • %Y: Four-digit year (e.g., 2021)
  • %m: Two-digit month (01-12)
  • %d: Two-digit day of the month (01-31)
  • %H: Hour in 24-hour format (00-23)
  • %M: Minutes (00-59)
  • %S: Seconds (00-59)
  • %A: Full weekday name (e.g., Wednesday)
  • %B: Full month name (e.g., March)

Example Command

To display the current date and time in a custom format, you can run:

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

This will output something like: 2024-08-02 09:21:03.

Feel free to experiment with different combinations of format specifiers to achieve the desired output!

0 Comments

no data
Be the first to share your comment!