To display the current date and time in a specific format in Bash, you can use the date command. The date command allows you to specify a format string that determines the format in which the date and time are displayed.

Advertisement

For example, to display the current date and time in the format “YYYY-MM-DD HH:MM:SS”, you can use the following command:

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

Here are some common format specifiers you can use in the format string:

  • %Y: Year with century as a decimal number (2022).
  • %m: Month as a decimal number (01-12).
  • %d: Day of the month as a decimal number (01-31).
  • %H: Hour in 24-hour format (00-23).
  • %M: Minute as a decimal number (00-59).
  • %S: Second as a decimal number (00-59).

Some other useful specifiers, you should also know:

  • %a: abbreviated weekday name (e.g., Sun).
  • %A: full weekday name (e.g., Sunday).
  • %b: abbreviated month name (e.g., Jan).
  • %B: locale’s full month name (e.g., January).
  • %D: date; same as %m/%d/%y .
  • %I: hour (01..12).
  • %p: locale’s equivalent of either AM or PM; blank if not known.
  • %U: Week number of year (00..53).

Let’s take another example, to display the current date in the format “DD/MM/YYYY”, you can use the following command:

date +"%d/%m/%Y" 

You can also use other format specifiers to display the date and time in different ways. For example, to display the current month and year in the format “Month Year”, you can use the following command:

date +"%B %Y" 

The %B represents the full name of the month.

You can find a full list of format specifiers on the `date` command’s man page. To view the man page, type `man date` at the command prompt.

Share.
Leave A Reply


Exit mobile version