<terminal>
/**
*    @Author: Arash Karimzadeh
*    @Email: me@arashkarimzadeh.com
*    @Desciption: Here, I post some of my recent
*    researches. Also you can see my code projects.
*/  
Ajax C # Chrome CMS dateNet Design Patterns includeMany JavaScript jBind Joomla jQuery Plugin rails ror RubyOnRails SQLite xul

+ All tags

Content View Hits : 313113
Bookmark and Share
Ruby on Rails Jalali Date (Shamsi Date) PDF Print E-mail
Written by Arash Karimzadeh   
Monday, 09 November 2009 09:33

This is Jalali-Gregorian date converter and also Jalali Date tag creator. Also it extends rails FormBuilder.

If you need this converter for your rails application just copy the jalali_date.rb file to your /lib/ directory and add below line to end of your environment.rb.

  1. require 'jalali_date'
 

You can also add Jalali_date_helper.rb to your /lib/ directory and below line to end of your environment.rb.

  1. require 'jalali_date_helper'
 

Download JalaliDate + JalaliDate Helper

Current version is for JalaliDate is 1.0.1 and JalaliDate Helper is 1.0.1
You can get both from here.

For date conversion (jalali to gregorian and vise versa) you can use it as below:
  1. Time.now  #=> Sat Nov 07 16:18:16 +0330 2009
  2. jalali_date = JalaliDate.to_jalali(Time.now)
  3. jalali_date.year  #=> 1388
  4. gregorian_date = jalali_date.to_gregorian #=> Sat Nov 07 00:00:00 +0330 2009
 

JalaliDate Class

Has following attributes:

day

[RW]  Returns the day of the month (1..n) for time.

month

[RW] Returns the month of the year (1..12) for time.

year

[RW] Returns the year for time (including the century).

wday

[RW] Returns an integer representing the day of the week, 0..6, with یکشنبه  == 0 .

And following methods

now()

Return current Jalali date .

to_gregorian(year,month=nil,day=nil)

Convert JalaliDate to Gregorian Date .

  1. JalaliDate.to_gregorian(1388,8,16) #=> Sat Nov 07 00:00:00 +0330 2009
  2. JalaliDate.to_gregorian({:year=>1388,:month=>8,:day=>16}) #=> Sat Nov 07 00:00:00 +0330 2009
 

 to_jalali(time)

This method will convert Gregorian date to Jalali and return JalaliDate instance.

  1. JalaliDate.to_jalali(Time.now)
 

strftime(ptr)

Formats date according to the directives in the given format string. Any text not listed as a directive will be passed through to the output string.
Format meaning:

  • %a - The abbreviated weekday name ’شن’
  • %A - The full weekday name ’شنبه’
  • %b - The abbreviated month name ’آب’
  • %B - The full month name ’آبان’
  • %d - Day of the month (01..31)
  • %m - Month of the year (01..12)
  • %y - Year without a century (00..99)
  • %Y - Year with century

  1. jalali_date.to_jalali Time.now
  2. jalali_date.strftime("Printed on %d/%m/%Y") #=> "Printed on 16/8/1388"
 
 

to_gregorian()

  1. jalali_date.to_gregorian #=> Sat Nov 07 00:00:00 +0330 2009
 
 

ActionView::Helpers::FormHelper

 jalali_date(object, method, date, options={}, html_options={})

Create a select tag for jalali date (needs utf-8 support) .

Options

  • :start_year - Set the start year for the year select. Default is passed_date.year - 5.
  • :end_year - Set the end year for the year select. Default is passed_date.year + 5.
  • :include_blank - Include a blank option in every select field so it‘s possible to set empty dates.

Html Options

Same as rails select tag html_options.

  1. <%= jalali_date :post, :submit_date, Time.mktime(1983,3,26) %>
  2. <%= jalali_date :post, :submit_date, Time.now, {:start_year=>1360, :end_year=>1400}, :class=>'date' %>
 
 

ActionView::Helpers::FormBuilder

jalali_date(method, options={}, html_options={})

Create a select tag for jalali date (needs utf-8 support).
The method is in Gregorian Format

Options

Same as FormHelper::jalali_date

Html Options

Same as rails select tag html_options

  1. <% form_for(@post) do |f| %>
  2.    <%= f.jalali_date :created_at, :prefix=>'start_date' %>
  3. <% end %>
  4. <% form_for(@user) do |f| %>
  5.    <%= f.jalali_date :birth, {:start_year=>1385, :end_year=>1390}, :class=>'date' %>
  6. <% end %>
 

Tags: rails | ror | RubyOnRails

Last Updated on Sunday, 15 November 2009 10:16
 
</terminal>