I write a lot of timing constants. It’s not really a lot of work, but they all kind of look like this:

The constants are named correctly enough, but I always have to do some quick math whenever I revisit my code. It’s just not very readable! How long is 1000 * 60 * 60 * 12 * 2, really? 🤷‍

Introducing timeproxy

timeproxy is a very tiny, blazingly fast and production ready library (835 bytes gzipped) that lets you write god damn beautiful constants! It uses a fancy “new” ES2015 feature called proxies to make your constants chill af. Don’t worry about the tech, just enjoy the dopeness:

This way, you can express your intent AND the amount of time, without having to deal with math at all. Also, it just reads so much better!

Edit: Thanks to some great feedback after this article was published, I’ve added support for using timeproxy as a tagged template literal as well:

Notice that you can use numbers or strings to specify your amounts — this even works with the “fake property” approach. Due to limitations in JavaScript, you can’t start a “fake property” with a number, though you can with tagged template literals.

Here’s a few more examples:

Another feature is relative time. If you want your timestamp to be relative to the current time, prefix with in or suffix with ago .

My goal with this library is that it will let you write your constants with natural language. Write what you feel like might make sense, and it will probably just work.

I hope you dig it!