1. Introduction

We sometimes encounter situations where we need to convert date formats from one format to another. It can be a tedious task; however,  the Linux shell offers us several powerful tools we can leverage to do the conversion.

In this tutorial, we’ll cover the different methods of converting a date from one format to another.

2. Using the date Command

The Linux date command is a powerful tool that allows us to display and manipulate the system’s date and time. It’s available by default in most Linux distros.

By default, the date command displays the date in the time zone on which the Unix/Linux operating system is set. We must ensure we have super-user privileges to change the system’s date and time.

Let’s use the date command to convert an input with a “YYYY-MM-DD” date format to “DD/MM/YYYY”:

$ date -d "2023-03-30" +'%d/%m/%Y'

We’re using the -d option to specify an input date and the + option to select the output format. Here, %d represents the day, %m the month, and %Y the year.

We can also specify a separator for the year, month, and day values. For example, here we’ve converted the separator from a hyphen (-) to a forward slash (/).

2.1. Converting Multiple Dates

We can also write a Bash script that automatically converts a list of dates. To achieve this, let’s first create a script file called multiple_dates.sh using the nano command:

$ nano multiple_dates.sh

Then we can add this content:

dates=("04/01/2023" "05/01/2023" "06/01/2023")
for d in "${dates[@]}"; do
  new_date=$(date -d "$d" +%Y-%m-%d)
  echo "$d converted to $new_date"

Once done, let’s save the file. Then, we can make it executable using the chmod command:

$ chmod u+x multiple_dates.sh

Let’s execute the multiple_dates.sh script:

$ ./multiple_dates.sh
04/01/2023 converted to 2023-04-01
05/01/2023 converted to 2023-05-01
06/01/2023 converted to 2023-06-01

We’re using a for loop to convert our list of dates that have the format “MM/DD/YYYY” to the format “YYYY-DD-MM”.

3. Using the awk Command

The Linux awk command is a multi-purpose tool that can be used to manipulate and process text data.

The awk command programming language doesn’t require compilation and allows us to use variables, strings, numeric functions, and logical operators.

For example, if we have a date in the format “YYYY-MM-DD”, and we want to convert it to the format “MM-DD-YYYY”. We can use this command:

$ echo "2023-03-31" | awk -F'-' '{printf("%s-%s-%s\n", $2, $3, $1)}'

We’re using the Linux echo command to output the date “2023-03-31”. Then, we’re using the awk command with the -F option to specify that the field separator is a hyphen “-“. 

Finally, we’re using the printf command to output the fields in the order we want with the separator we want. We can change the format by modifying the positions of the $1, $2, and $3 variables.

4. Using the sed Command

The Linux sed command can perform a lot of functions, such as searching, replacing, and transforming text data. However, the most popular use of the sed command in Linux is for substitutions or find and replace.

To convert a date format using the sed command, we can use regular expressions to extract the year, month, and day from the input date. We can then use the replace() function to format the output.

For example, when we have a date in the format “DD/MM/YYYY”, we can convert it to the “YYYY/MM/DD” format using the following command:

$ echo "31/03/2023" | sed -E 's/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/\3\/\2\/\1/'

We’re using the -E option to use the extended regular expressions of sed. It allows us to use group capture to extract the month, day, and year values.

The regular expression ([0-9]{2})\/([0-9]{2})\/([0-9]{4}) matches the date format “MM/DD/YYYY” and uses three capture groups to extract the month, date and year values.

The replacement string /\3\/\2\/\1/ rearranges the capture groups to match the format “YYYY/MM/DD”.

5. Conclusion

In this article, we’ve explored different methods we can use to change dates to different formats, with the date command being the easiest. We’ve also looked at how we can write a Bash script that auto-converts a list of input dates.

We can use any of these methods to convert any date to a different format. The method to use mostly depends on our preference or the tools available.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.