ISO 8601 date format in bash shell script? yyyy-MM-dd T HH:mm:ss Z

How to get ISO 8601 date format – yyyy-MM-dd T HH:mm:ss Z – printed in bash?

The bash shell provides a built-in option to print the date format in yyyy-MM-dd T HH:mm:ss Z (with the offset from UTC, such as UTC+05:30 for India).

date -Is

This will format ‘date’ for date only (the default), ‘hours’, ‘minutes’, ‘seconds’, or ‘ns’ for date and time

From bash – date man page

-I[FMT], –iso-8601[=FMT]
output date/time in ISO 8601 format. FMT=’date’ for date only (the default), ‘hours’, ‘minutes’, ‘seconds’, or ‘ns’ for date and time to the indicated precision. Example:
2024-01-01T02:34:56-06:00

An example output would look like this:

dev@codetryout:~$ date -Is
2024-01-01T11:53:11+05:30

You may also refer to other most popular date printing formats in bash:

How to echo ISO 8601 format (YYYY-MM-DD) in bash

To echo the current date in ISO 8601 format (YYYY-MM-DD) in a Bash shell script, you can use the date command with the appropriate format specifier. Here’s an example:

date +%F

Example output

2024-01-01

In the above script, the date +%F command uses the %F format specifier to represent the date in the ISO 8601 format (YYYY-MM-DD).

You can modify the script per your requirements and incorporate the ISO 8601 date format wherever needed.

What is the ISO 8601 date format?

ISO 8601 is an international standard for representing dates and times. The ISO 8601 date format is a way of expressing dates and times in a standardized and unambiguous way. It follows the YYYY-MM-DD format for dates and HH:MM:SS for times, with an optional time zone offset.