|
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. require 'jalali_date'
require%20%27jalali_date%27 You can also add Jalali_date_helper.rb to your /lib/ directory and below line to end of your environment.rb. require 'jalali_date_helper'
require%20%27jalali_date_helper%27 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:
Time.now #=> Sat Nov 07 16:18:16 +0330 2009 jalali_date = JalaliDate.to_jalali(Time.now) jalali_date.year #=> 1388 gregorian_date = jalali_date.to_gregorian #=> Sat Nov 07 00:00:00 +0330 2009
Time.now%C2%A0%20%23%3D%3E%20Sat%20Nov%2007%2016%3A18%3A16%20%2B0330%202009%0Ajalali_date%20%3D%20JalaliDate.to_jalali%28Time.now%29%0Ajalali_date.year%C2%A0%20%23%3D%3E%201388%0Agregorian_date%20%3D%20jalali_date.to_gregorian%20%23%3D%3E%20Sat%20Nov%2007%2000%3A00%3A00%20%2B0330%202009
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 . JalaliDate.to_gregorian(1388,8,16) #=> Sat Nov 07 00:00:00 +0330 2009 JalaliDate.to_gregorian({:year=>1388,:month=>8,:day=>16}) #=> Sat Nov 07 00:00:00 +0330 2009
JalaliDate.to_gregorian%281388%2C8%2C16%29%20%23%3D%3E%20Sat%20Nov%2007%2000%3A00%3A00%20%2B0330%202009%0AJalaliDate.to_gregorian%28%7B%3Ayear%3D%3E1388%2C%3Amonth%3D%3E8%2C%3Aday%3D%3E16%7D%29%20%23%3D%3E%20Sat%20Nov%2007%2000%3A00%3A00%20%2B0330%202009 to_jalali(time)This method will convert Gregorian date to Jalali and return JalaliDate instance. JalaliDate.to_jalali(Time.now)
JalaliDate.to_jalali%28Time.now%29 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
jalali_date.to_jalali Time.now jalali_date.strftime("Printed on %d/%m/%Y") #=> "Printed on 16/8/1388"
jalali_date.to_jalali%20Time.now%0Ajalali_date.strftime%28%22Printed%20on%20%25d%2F%25m%2F%25Y%22%29%20%20%23%3D%3E%20%22Printed%20on%2016%2F8%2F1388%22 to_gregorian()jalali_date.to_gregorian #=> Sat Nov 07 00:00:00 +0330 2009
jalali_date.to_gregorian%20%23%3D%3E%20Sat%20Nov%2007%2000%3A00%3A00%20%2B0330%202009
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. <%= jalali_date :post, :submit_date, Time.mktime(1983,3,26) %> <%= jalali_date :post, :submit_date, Time.now, {:start_year=>1360, :end_year=>1400}, :class=>'date' %>
%3C%25%3D%20jalali_date%20%3Apost%2C%20%3Asubmit_date%2C%20Time.mktime%281983%2C3%2C26%29%20%25%3E%0A%3C%25%3D%20jalali_date%20%3Apost%2C%20%3Asubmit_date%2C%20Time.now%2C%20%7B%3Astart_year%3D%3E1360%2C%20%3Aend_year%3D%3E1400%7D%2C%20%3Aclass%3D%3E%27date%27%20%25%3E
ActionView::Helpers::FormBuilderjalali_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 <% form_for(@post) do |f| %> <%= f.jalali_date :created_at, :prefix=>'start_date' %> <% end %> <% form_for(@user) do |f| %> <%= f.jalali_date :birth, {:start_year=>1385, :end_year=>1390}, :class=>'date' %> <% end %>
%3C%25%20form_for%28%40post%29%20do%20%7Cf%7C%20%25%3E%0A%C2%A0%C2%A0%20%3C%25%3D%20f.jalali_date%20%3Acreated_at%2C%20%3Aprefix%3D%3E%27start_date%27%20%20%25%3E%0A%3C%25%20end%20%25%3E%0A%3C%25%20form_for%28%40user%29%20do%20%7Cf%7C%20%25%3E%0A%C2%A0%C2%A0%20%3C%25%3D%20f.jalali_date%20%3Abirth%2C%20%7B%3Astart_year%3D%3E1385%2C%20%3Aend_year%3D%3E1390%7D%2C%20%3Aclass%3D%3E%27date%27%20%25%3E%0A%3C%25%20end%20%25%3E
Tags: rails | ror | RubyOnRails
|
|
Last Updated on Sunday, 15 November 2009 10:16 |