Is there a command-line tool to convert/format time? (I mean time, not date!)

Here is what I was expecting:

$ timeconverter --format '%s' 1h
$ timeconverter --format '%h:%m:%s' 3665s
$ timeconverter --format '%H:%m:%s' 3665s
$ timeconverter --format '%Hh%Mm%Ss' 3966s
$ timeconverter --format '%m' 2d
$ timeconverter --format '%d' 1w
$ timeconverter --format '%y' 365d
$ timeconverter --format '%s' 1h10m

Where %m is minute, %d is day, %y is year, %h is hour, %s is second, %w is week, and so on.

I myself could write a shell script for it, but it is very likely that someone else already did that, and I want to save my time.


You have the GNU software units (available as a package units in many Linux distributions).

Here is simple run to find out the number of days in 2 weeks:

$ units "2 week" day
* 14
/ 0.071428571

The manpage explains in full detail the input, output, exit code, and other ways to use the command.


Don't think there's an established command line tool ready for that. The main reason might be that doing this might come with a plethora of pitfalls, due to human time not being very ... linear.

Example: you want to know the number of minutes in the next 2 years. That number depends on whether 2022 or 2023 are leap years.

If you really just want this with "standard" units (i.e. ignoring reality):

I probably wouldn't implement this as a shell script, but as a very minimal /usr/bin/env python3 -I -S script that converts the input argument to seconds, and then just uses passes the format string to the string formatting function after calculating the couple of integer divisions and modulos necessary for all potential fields you might want to output.

Anyways, let me repeat that: there are a few, but not very many, good reasons you might want to do this. Generally, conversion of time periods becomes hazardous if you don't know what the result will be used for; printing "your alarm clock will go off in 600 minutes" when in fact it goes off in 9hrs at 8am after the clock adjustment at daylight savings time is a bad idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.