sunil sunil - 6 months ago 39
Javascript Question

Which plugin used by gmail to show mail time or date?

I wanted to know that which js plugin used by gmail to show mail time(if today's mail) or date.

If mail received today itself then it shows time '1:18 pm' otherwise date like 'sep 5'. And if mail was received i last year then it showing date like '11/12/13'.

Is there any js plugin or rails to do that ?
Like this enter image description here

Thanks

Answer

why you need any plugin or gem for this. You can easily do it with helper. In your helper :

require 'date'

def time_date(date)
  if date.year > Time.now.year
    date.strftime('%d/%m/%y')
  else 
    date.to_date == Time.now.to_date ? date.strftime('%l:%M %P') : date.strftime('%b %d')
  end
end

in your view :

<%= time_date(@model.created_at) %>

As per your comment You do want to use jquery to show datetime instead of helper. So there is no direct plugin for this. But you can use this https://github.com/agschwender/jquery.formatDateTime to achieve what you want.

Lets consider on your view you have following div:

<div class="start">2014/09/11 09:00:03</div>

Now use jquery.formatDateTime to format div date :

$(document).ready(function(){
  var datetime = new Date($('.start').text());
  var today = new Date();
  if(today.getFullYear() > datetime.getFullYear()){
    $('.start').formatDateTime('mm/dd/y');
  }
  else{
    if(today.getDate() > datetime.getDate()){
      $('.start').formatDateTime('M m');
    }
    else{
      $('.start').formatDateTime('g:ii a');
    }
  }

});

Hope this help you!

Comments