Converting a date into a string

Converting a date into a string

How to:

Below are examples of how to convert a date to a string in Bash:

# Display the current date and time in the default format
echo $(date)

# Custom format: YYYY-MM-DD
echo $(date '+%Y-%m-%d')

# Include the time
echo $(date '+%Y-%m-%d %H:%M:%S')

# Convert an existing date
existing_date='2023-03-17 08:00:00'
date -d "$existing_date" '+%A, %B %d, %Y'

Sample output for the commands above:

Sat Mar 25 12:04:22 PDT 2023
2023-03-25 12:04:22
Friday, March 17, 2023

Deep Dive

Unix-like systems have used the date command from early on for handling date and time. Its flexibility allows for a myriad of formats, courtesy of format specifiers like %Y for year and %d for day.

There are alternatives to the date command if you’re using a different tech stack. For instance, Python has datetime.strftime, while JavaScript offers the Date object with methods like toLocaleDateString().

When converting dates in Bash, remember that the date command can work with the system’s current timestamp or a provided date. Timezone handling is also crucial for accurate date conversions.

See Also