Skip to content

Instantly share code, notes, and snippets.

@kt0
Last active November 19, 2016 16:34
Show Gist options
  • Save kt0/8499005 to your computer and use it in GitHub Desktop.
Save kt0/8499005 to your computer and use it in GitHub Desktop.
Persian date and numbers in ruby / ruby on rails

For persian_date add jalalidate to your gemfiles, then add this to ApplicationHelper as helper (or any other place you want to use).

Default date format here is like (یک‌شنبه ۲۹ دی ۱۳۹۲ - ۰۳:۱۰) for more format options see.

unpack Snippet from a question in stackoverflow

module ApplicationHelper
  def persian_numbers(str)
    str ? str.unpack('U*').map{ |e| (0..9).to_a.include?(e-48) ? e + 1728 : e }.pack('U*') : ''
  end

  def persian_date(date)
    date ? persian_numbers(JalaliDate.new(date).format("%A %d %b %Y - %H:%m")) : ''
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment