๐Ÿ•ฐ๏ธ How to Print Local Time in ISO 8601 Format, For the Lazy ๐Ÿ•ฐ๏ธ

July 25, 2021

Hey there! Are you tired of searching for an easy way to print local time using the ISO 8601 format? Look no further! As a lazy blogger myself, I understand the need for a quick and effortless solution. In this post, we'll explore how to print local time in ISO 8601 format with minimal effort using the date command.

The Problem: Printing Local Time in ISO 8601 Format

When it comes to printing local time in ISO 8601 format, things can get a bit tricky. The time function seems like an obvious choice, but it actually times the arguments passed to it. That's great for timing functions and such, but not so useful when we just want to print the current local time.

The Solution: Using the date command

That's where the date command comes in! This versatile utility can display or set the date and time, and it does so with ease. But, we need a bit of formatting to make it look like ISO 8601 format. Don't worry; it's easy to do!

Adding Formatting with a Custom Function

To make things simple, let's create a custom function in our ~/.zshrc file (if you're using zsh as your shell). This function will format the current local time in ISO 8601 format. Here's the code:

function thetime() { date +%Y-%m-%dT%H:%M:%S; }

Save and close the file, and you're ready to go!

Using the Custom Function

Now, open a new terminal window or refresh the existing one with source .zshrc. We're almost there! To print the current local time in ISO 8601 format, simply type:

thetime

Boom! Done.

๐Ÿ“ท Visual Proof of Concept

To show you that it works, here's a screenshot of the output:

The Time

As you can see, the current local time is printed in ISO 8601 format, complete with the year, month, day, hour, minute, and second. Easy peasy!

Conclusion

There you have it, folks! With this simple trick, you can print local time in ISO 8601 format with minimal effort. No more searching for complicated solutions or writing your own code. Just a few lines of code in your ~/.zshrc file, and you're good to go!

So, what are you waiting for? Give it a try and let me know how it goes in the comments below!ยน Happy blogging! ๐Ÿ˜Š

ยนWhen I implement comments, that is. ๐Ÿ˜…

Updated: December 24, 2023


Profile picture

Written by Null who lives and works in the Valley building cool things. You should follow them on Twitter

Tags: code zsh

ยฉ 2023, null.guru