0

I have a chat, and I'm appending a timestamp for when comment a is made.

This is how Im doing it using Pusher and Laravel 5.2:

channel.bind('App\\Events\\MessageSent', function(data){
    console.log(data);
    $('#chat-box-message').empty(html);
    for (var i = 0; i< data.length; i++) {

        for (var key in data) {  //empty the div for append does not repeat
            var obj = data[key];
            for (var prop in obj) {

                $('#chat-box-message').append(
                    '<div class="comment">'+
                    ' +obj[prop]['gamertag']+
                    '<div class="metadata">'+
                    + moment(obj[prop]['created_at']).format('h:mm:ss a') +
                    '</div>'
                );
            }
        }
    }
});

When I type something in the chat, this is the result I get from the other users screen.

My Comment with Date

How can I adjust the time to be my current time, which was 8:26 PM when that comment was made.

I'm using the moment JS Library.

Or even better yet, show relative time, like say the comment was posted 5 min ago.

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
David
  • 1,987
  • 5
  • 33
  • 65
  • By default, moment parses and displays in local time, if you need you can use [`moment.utc()`](http://momentjs.com/docs/#/parsing/utc/) to use UTC or [moment-timezone](http://momentjs.com/timezone/) to convert you moment object to given timestamp. Note that moment has the [`fromNow`](http://momentjs.com/docs/#/displaying/fromnow/) method to display relative time. – VincenzoC Sep 08 '16 at 10:41

1 Answers1

1

When you broadcast a message, you should get the current time. Then you can send this as a field in the message data. If you want to to show the "time since", you will have to convert the time to a Date (if it isn't already) and then subtract the time in the message from the current time, as described here.

Community
  • 1
  • 1
Will Sewell
  • 2,593
  • 2
  • 20
  • 39