开发者

I would like to build a current month calendar and be able to browse back month and forward months

mainly I just need to know what the commands are to get month, day and year, first day of month and last day of month. I should be able to figure it out from there. I've built a nice one in PHP but I would rather use ruby since using a database is so much easier i开发者_如何学Gon ruby.

So if you can point me in the right direction.


ActiveSupport (part of the Rails framework, but can be used separately) extends the standard library Date class with features fit for your purpose. If you don't have Rails already installed, just install ActiveSupport:

gem install activesupport

Then in your code you can use it like this:

require 'rubygems'
require 'active_support'

# Get today's date
today = Date.today

# These are in the standard library Date class
today.year
 => 2010 
today.month
 => 7
today.day
 => 10 
today.wday
 => 6 

# These are added by ActiveSupport
date = today.beginning_of_month
 => Thu Jul 01 2010
date.end_of_month
 => Sat Jul 31 2010
date.prev_month
 => Tue, 01 Jun 2010 
date.next_month
 => Sun, 01 Aug 2010 
date + 2.months
 => Wed, 01 Sep 2010 
date - 2.months
 => Sat, 01 May 2010 

See the documentation for details.


Why you want to create when there is already plugins for that ref CalendarDateSelect Demo for mini-calendar AND event_calendar for event showing in caledar format.


http://ruby-doc.org/core/classes/Date.html describes Ruby's Date class. The wday function will get the day-of-the-week of an arbitrary Date.

http://www.techotopia.com/index.php/Working_with_Dates_and_Times_in_Ruby describes how to use Dates in their entirety.


Also checkout the cal gem. It only does the basics for you so you can focus on building the calendar yourself. Stuff like determining the days in the calendar (usually a few days from the previous month, the days in the current month, and a few days from the next month) and starting the week on a day other than Sunday.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜