ryanlowe / client_date

Uses client-side Javascript to format a datetime instead of the Ruby on Rails server.

This URL has Read+Write access

client_date / lib / client_date.rb
100644 29 lines (25 sloc) 0.678 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module ClientDate
  def format_date(date)
    return "" if date.nil?
    assemble_javascript(date,"fd")
  end
  
  def format_datetime(date,seconds=false)
    return "" if date.nil?
    function = seconds ? "fdts" : "fdt"
    assemble_javascript(date,function)
  end
  
  private
  
    def date_params(date)
      return "" if date.nil?
      text = date.year.to_s + ","
      text += date.month.to_s + ","
      text += date.day.to_s + ","
      text += date.hour.to_s + ","
      text += date.min.to_s + ","
      text += date.sec.to_s
      text
    end
  
    def assemble_javascript(date,function)
      "<script>"+function+"("+date_params(date)+");</script>"
    end
end