Format Date to yyyymmdd in Bash

Bash format date to yyyymmdd

Using date Command

Use the date command to format the date to yyyymmdd in Bash.

In the above example, the bash date command is used to get the current date with the format specifier '%Y%m%d' to specify the date format. In Bash, the date command is used to retrieve the date and time in this format Wed, May 31, 2023 6:06:35 PM, but we wanted to get the date only.

The + followed by the format string '%Y%m%d' specifies the desired date format. Here, %Y represents the four-digit year, %m represents the zero-padded month, and %d represents the zero-padded day of the month. By combining them (%Y%m%d), we will get the desired date format yyyymmdd.

Have a look at another example:

In this example, +%F is an alias of '%Y-%m-%d' used to get the current date in the yyyy-mm-dd format. Then, pipe operator | is used to pass the current date to the tr command with the -d option to delete or remove the hyphens (-) from the output of the date. With this modification, the output of the formatted_date variable is displayed in the desired format yyyymmdd without any hyphens.

Using printf Command

Use the printf command to format the date to yyyymmdd in Bash.

In this bash code, the printf command is used to format the current date to the desired format (%Y%m%d). For example, in the format specifier %T, the T is not a placeholder for any specific date or time component. Instead, it is a literal character that separates the date and time components in the format string.

If you want to get both date and time, use this format %Y%m%dT%H%M%S where %H represents the zero-padded hour (24-hour format), %M represents the zero-padded minute, and %S represents the zero-padded second.

In the bash >=4.2 version, utilising the printf command’s built-in date formatting is recommended instead of relying on the external date command. The advantage of using printf is that it is significantly faster as a Bash built-in, whereas date is an external command.

Have a look at another example below:

In this example, IFS is used with printf to format the date. First, the IFS sets the hyphen character (-) as an internal field separator and the read command is used to read the input date 31-05-23 in the variables d, m and y respectively. In Bash, <<< is a here-string redirection that allows passing a string as the input to the command. After that, the printf command is used to format and print the date.

Now let’s understand what this '%.4d%.2d%.2d\n means:

  • %.4d specifies that the variable should be printed as an integer with at least four digits, zero-padded if necessary.
  • %.2d specifies that the variable should be printed as an integer with at least two digits, zero-padded if necessary.
  • "${y#0}", "${m#0}", and "${d#0}" are parameter expansions that remove any leading zeros from the variables y, m, and d respectively.

Finally, the formatted date is printed, resulting in the format yyyymmdd

Using awk Command

Use the awk command to format the date to yyyymmdd in Bash.

In this example, the awk command is used to format the input date string to the desired format. Here, the first user is prompted to enter the date. Then, the read command reads the user input and stores it in the X variable. Then, the output of the echo command is passed to the awk using the pipe | operator.

In awk, the -F parameter sets the field separator to the hyphen character (-), meaning each date component separated by - is a separate field. Next, the print command prints each field in the YYYYMMDD format. The '{print $3$2$1}' part instructed the awk to print the third field (year), followed by the second field (month), and finally, the first field (day), effectively rearranging the date components.

Using sed Command

Use the sed command to format the date to yyyymmdd in Bash.

In this bash code, the sed command formats the given 31-05-23 date to the desired 20230531 format. Here, the first echo command provides the input date 31-05-2023 to the sed command as a string. Then, the sed command performs the substitution operation and outputs the formatted date.

In Bash, the sed is a stream editor that performs text transformations on input. The -E option in sed enables the extended regular expressions in sed.

  • 's/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})$/\3\2\1/' is the substitution pattern used by sed.
  • Then, the pattern ^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})$ matched a date in the format dd-mm-yyyy and captured the day, month, and year components.
  • Afterwards, the replacement pattern \3\2\1 rearranged the captured components to yyyymmdd format.

Finally, the formatted date is printed, resulting in the format yyyymmdd.

That’s all about format date to yyyymmdd in Bash.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *