Skip to content

Instantly share code, notes, and snippets.

@ryanjm
Created April 6, 2012 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanjm/2320468 to your computer and use it in GitHub Desktop.
Save ryanjm/2320468 to your computer and use it in GitHub Desktop.
Special variables for dates
# == Schema Information
#
# Table name: showings
#
# id :integer not null, primary key
# start_time :datetime
# end_time :datetime
class Showing < ActiveRecord::Base
attr_accessor :showing_date, :start_date, :end_date
validates :showing_date, :start_date, :end_date, :presence => true
before_save :set_dates
# used to set the instance variables for editing the form
after_find :set_times
def set_times
self.showing_date = self.start_time.strftime("%A, %B %d, %Y") unless self.start_time.nil?
self.start_date = self.start_time.strftime("%-I:%M%p") unless self.start_time.nil?
self.end_date = self.end_time.strftime("%-I:%M%p") unless self.end_time.nil?
end
private
def set_dates
self.start_time = DateTime.strptime("#{start_date} #{showing_date} #{Time.now.zone}", "%I:%M%p %A, %B %d, %Y %z")
self.end_time = DateTime.strptime("#{end_date} #{showing_date} #{Time.now.zone}", "%I:%M%p %A, %B %d, %Y %z")
end
end
@amnesia7
Copy link

amnesia7 commented May 2, 2012

I'm starting to give up on the idea of seperate date and time fields so I've started converting my app to use a single datetime field/db column to see if that is a better option for me.

I'm now on the lookout for a datetime picker since I need the datetime to be in a textfield but I don't think the date part and time part can be split easily. Before I was using a datepicker on a textfield for the date and dropdowns for the time.

@amnesia7
Copy link

In the end I used a merged datetime text_field and the code here: codegram/date_validator#25 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment