Skip to content
This repository was archived by the owner on Dec 22, 2020. It is now read-only.

Add a time-ago widget? #67

Open
ckcollab opened this issue Nov 27, 2015 · 3 comments
Open

Add a time-ago widget? #67

ckcollab opened this issue Nov 27, 2015 · 3 comments

Comments

@ckcollab
Copy link

Ah, sorry hit post on accident! Updating...

I just whipped this up for a personal project using RiotJS and I thought you guys might find it useful. Apologies for not taking the time to fork the repo and such, I'm normally a backend guy so it's kind of daunting. I may do just that if I get some more free time this weekend.

Usage:
<time-ago timestamp="2015-11-27T04:02:30.935205+00:00"></time-ago>

Example output:
5m ago

<time-ago>
    <span show={caption}>{ caption } ago</span>

    <script>
        var self = this;

        function timeSince(date) {
            var seconds = Math.floor((new Date() - date) / 1000);
            var interval = Math.floor(seconds / 31536000);

            if (interval > 1) {
                return interval + "yr";
            }
            interval = Math.floor(seconds / 2592000);
            if (interval > 1) {
                return interval + "mon";
            }
            interval = Math.floor(seconds / 86400);
            if (interval > 1) {
                return interval + "d";
            }
            interval = Math.floor(seconds / 3600);
            if (interval > 1) {
                return interval + "hr";
            }
            interval = Math.floor(seconds / 60);
            if (interval > 1) {
                return interval + "m";
            }
            return Math.floor(seconds) + "s";
        }

        var update_caption = function() {
            self.date = new Date(self.opts.timestamp);
            self.caption = timeSince(self.date);
            self.update();
            window.setTimeout(update_caption, 15000);
        };

        if(self.opts.timestamp !== undefined) {
            update_caption();
        }
    </script>

</time-ago>
@gregorypratt
Copy link
Contributor

Yeah nice. Might expand on this idea and make use of moment.js...

@ckcollab
Copy link
Author

Oh man, I knew there was some library that already did this better than I could :D

Yeah moment JS "Relative Time" seems perfect, is it OK to add dependencies to this project like that?

EDIT:
Oh, it's already a dependency, I see... maybe I should try forking this/learning more about Node projects.

@ckcollab ckcollab mentioned this issue Nov 28, 2015
7 tasks
@gregorypratt
Copy link
Contributor

I'm thinking create a <rg-moment> tag that essentially wraps the majority of the moment API - as far as moment dependency is concerned its fine to depend on a separate library.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants