Last active
December 17, 2015 04:19
-
-
Save delba/5550019 to your computer and use it in GitHub Desktop.
Setting relative date with moment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //= require jquery | |
| //= require jquery_ujs | |
| //= require turbolinks | |
| //= require moment | |
| //= require_tree . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| timeElements = document.querySelectorAll('[data-behavior~=relative_date]') | |
| for el in timeElements | |
| el.moment = moment(el.getAttribute('datetime') | |
| el.update = -> @innerText = @moment.fromNow() | |
| el.update() | |
| setInterval (-> el.update() for el in timeElements), 60000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ItemsHelper | |
| def relative_date(date) | |
| options = { datetime: date.iso8601, data: { behavior: 'relative_date' } } | |
| content_tag :time, options do | |
| "#{time_ago_in_words(date)} ago" | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Created <%= relative_date @item.created_at %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment