Relative Time Formatter Assignment
In How long has it been I talked a bit about representing relative time in software. So, here's an assignment that can be as simple or as complex as you want to make it: write a function that returns a relative 'time ago' natural language string - either between two dates, or between another date and now.
Considerations
As with most programming problems, there is no single correct answer. You can use the basic idea as a starting point, and expand in whatever direction you like.
Here are a few decisions you might need to make, in no particular order:
- Which programming language will you use?
- And which language features or libraries?
- How will your algorithm decide what to return?
- Which human language(s) will you support?
- What level of granularity will you return?
- How will you handle pluralization?
- How will you handle precision and rounding?
- What types of time units will you support?
- What level of customization will you support, and how?
- What types of inputs will you support?
- How will you handle errors, like invalid inputs or configurations?
- Will you create a standalone utility, or part of a broader utility library?
- Will you create a UI component, or only a utility?
- How will you test?
- How will you document?
- What performance optimizations might you consider?
Maximizing Your Learning, and Getting Help
Not sure where to start? Here are some tips to maximize your learning, while still getting some help, in the age of AI.
Extra Bonus Assignment
If you want to raise your challenge level considerably, you could also consider a relative time parser. For example, given a string like three days and seven hours ago, return an absolute date.
Sharing Some Examples
Later, I'll share some examples I've found. I'll start with examples from the JavaScript ecosystem, but I'll look for a few from other languages as well.