2 minute read

A younger brother knows his older brother better than anyone else.

Mastering Date Formatting in Bash: A Developer’s Guide

As developers, we often find ourselves wrestling with dates and times in our scripts. Whether you’re logging events, processing data, or just trying to add a timestamp to your output, understanding how to format dates in Bash is an essential skill. In this guide, we’ll dive into the art of date formatting in Bash, unlocking the secrets of the date command and exploring its vast potential.

The Power of date

At the heart of date manipulation in Bash lies the unassuming date command. Don’t let its simplicity fool you – this command is a Swiss Army knife for time-related operations. Let’s start with the basics:

date

Running this command will output the current date and time. But that’s just scratching the surface. The real magic happens when we start customizing the output.

Crafting Custom Date Formats

To format dates in Bash, we use the + symbol followed by format specifiers. Each specifier starts with a % symbol. It’s like a secret code that tells Bash exactly how we want our date to look. For example:

date "+%Y-%m-%d"

This command will output the date in the familiar YYYY-MM-DD format. But why stop there? Let’s look at some of the most useful format specifiers:

Specifier Meaning Example
%Y Year (4 digits) 2024
%m Month (01-12) 09
%d Day of month (01-31) 28
%H Hour (00-23) 14
%M Minute (00-59) 30
%S Second (00-59) 55
%A Full weekday name Saturday
%B Full month name September

Date Formatting in Action

Now that we have our toolbox of specifiers, let’s put them to use with some practical examples:

# Current date and time
date "+It's %H:%M:%S on %A, %B %d, %Y"
# Output: It's 14:30:55 on Saturday, September 28, 2024

# Perfect for log files
date "+[%Y-%m-%d %H:%M:%S]"
# Output: [2024-09-28 14:30:55]

# Unix timestamp (seconds since 1970-01-01)
date "+%s"
# Output: 1727455855

Time Travel with Bash

Believe it or not, the date command allows us to perform some date arithmetic. Want to know the date a week from now? No problem:

date -d "+1 week" "+%Y-%m-%d"

Or maybe you need to know what day of the week your birthday falls on next year:

date -d "2025-04-15" "+Your birthday will be on a %A"

Timezone Tango

Working with different timezones? Bash has got you covered:

TZ='America/New_York' date

This command will show you the current date and time in New York, regardless of your system’s timezone.

The Quirks and Perks

It’s worth noting that some of these options (especially -d for date arithmetic) might behave differently or not work at all on non-GNU systems like macOS. If you’re writing cross-platform scripts, you might need to get creative or use additional tools.

Wrapping Up

Mastering date formatting in Bash opens up a world of possibilities for your scripts. From simple timestamps to complex date calculations, the date command is a powerful ally in your development toolkit.

Remember, practice makes perfect. Try incorporating these date formatting techniques into your next Bash script and watch as your output becomes more informative and professional.

Happy coding, and may all your dates be perfectly formatted!


Do you have any favorite date formatting tricks in Bash? Share them in the comments below!

–HTH–

Updated: