date snippets

How to integrate the Any+Time jQuery date-select plugin with Rails

Tagged date, time, select, anytime, ruby, rails  Languages ruby

Add the following to your AR model:

def send_at_formatted
   self.send_at.strftime('%d.%m.%Y') if self.send_at?
end

def send_at_formatted=(value)
   self.send_at = Time.zone.parse(value)
end

Add the following to your JavaScript file:

$(document).ready(function() {
  $('.date-select').each(function(index, element) {
    var elem = $(element),
        options = { format: '%d.%m.%Y', labelTitle: elem.data('title') };

    AnyTime.picker( elem.attr('id'), options );
  });
...

Add this to your HAML/ERB view:

= f.text_field :send_at_formatted, :class => 'date-select', :"data-title" => 'Send at'

Download Any+Time JS and CSS files and include them in your head tag.

Ruby and Ruby on Rails Time and Time Zones

Tagged ruby, time, date, timezone, use_zone, dst, current, local, now  Languages 

Checklist

  • Parsing: What is the time zone of the input?
  • Parsing: Use Time.zone.parse to parse user input, but first set Time.zone to the user’s time zone.
  • UI: What is the user’s time zone?
  • UI: Show the time in the user’s time zone: https://github.com/github/time-elements
  • Ruby: Time.now uses the operating system’s time zone
  • Ruby: Date.today uses the operating system’s time zone
  • Ruby: Time.parse uses the operating system’s time zone
  • Ruby: Use Range.cover? with time ranges and know how it differs from Range.include?
  • Rails: Time.zone.now and Time.current uses Rails’ time zone (config.time_zone in config/application.rb)
  • Rails: Time.zone.today uses Rails’ time zone
  • Rails: Time.zone.parse uses Rails’ time zone
  • Database: Store time in UTC (without time zone information)
  • Database: “BETWEEN a AND b” is inclusive
  • Database: PostgreSQL’s current_timestamp uses the database time zone (timezone in postgresql.conf)
  • API: Use ISO 8601 format
  • Daylight savings
  • Leap year
  • Falsehoods