timex
timex copied to clipboard
Relative/Calendar time formatters/parsers
These two are common use cases for formatting dates/times, so I'd like to add them as part of Timex rather than relying on plugins for them.
- [x] Relative time formatter
- [ ] Calendar time formatter
Relative Time (based on Moment.js)
| Range | Key | Sample Output |
|---|---|---|
| 0 to 45 seconds | s | a few seconds ago |
| 45 to 90 seconds | m | a minute ago |
| 90 seconds to 45 minutes | mm | 2 minutes ago ... 45 minutes ago |
| 45 to 90 minutes | h | an hour ago |
| 90 minutes to 22 hours | hh | 2 hours ago ... 22 hours ago |
| 22 to 36 hours | d | a day ago |
| 36 hours to 25 days | dd | 2 days ago ... 25 days ago |
| 25 to 45 days | M | a month ago |
| 45 to 345 days | MM | 2 months ago ... 11 months ago |
| 345 to 545 days (1.5 years) | y | a year ago |
| 546 days+ | yy | 2 years ago ... 20 years ago |
Calendar Time (based on Moment.js)
| Range | Output |
|---|---|
| Last week | Last Monday at 2:30 AM |
| The day before | Yesterday at 2:30 AM |
| The same day | Today at 2:30 AM |
| The next day | Tomorrow at 2:30 AM |
| The next week | Sunday at 2:30 AM |
| Everything else | 7/10/2011 |
I'm using the relative formatter to show how long ago articles were submitted to a database. If they submit the article themselves, on the next page the relative date formatter displays "In 0 seconds", since there was a time difference of 0, which doesn't make much sense since in the context.
It should probably display something like "Right now". Moment.js puts in "a few seconds ago" despite the fact its 0. Either case would better than it is now for me.
Thanks!
Thanks for the feedback @mikelat, sorry for the delay. I'll get that addressed in the next release :)
Just wanted to say thanks for this. Any idea when the relative parser will be available? I looked at the branch and would love to contribute. Wasn't sure of the status though.
@binarylogic once the release for Elixir 1.3 drops on Wednesday I'd be glad to provide guidance on the implementation, currently there is not a relative parser in progress, but building one is fairly straightforward :)
Absolutely, I'd love to knock that out. I have a project that needs it. Let me know how / when you want to talk and I'll get started.
Also, elixir 1.3 was just recently released.
@binarylogic yeah I mean the Timex release for 1.3 ;)
@binarylogic 1.3 introduced calendar types, so the types Timex introduced a long time ago are no longer necessary. Because of the new calendar API, I'm refactoring Timex to use those types instead, hence what I mean by "Timex release for 1.3".
Awesome, sounds great. I'll keep an eye out and jump in when that's released.
@bitwalker, is this still an active issue?
It is, I just haven't had the time or need to work on this yet. It is definitely something I'd be glad to merge if someone wants to take on the work to put together a PR!
@bitwalker this looks like something I would like to implement. Do you have any insights or suggestions before I jump into working on it?