Search code examples
ruby-on-railsrubytimedateactivesupport

Get Time Object at Start of Day in a Particular Time Zone


How can I get a ruby Time object that represents the start of the day on a particular date in a given timezone.


Solution

  • I ended up using the #local method on the ActiveSupport::TimeZone object passing components of the Date object.

    # Get example date and time zone...
    date = Date.today
    timezone = ActiveSupport::TimeZone['America/New_York']
    
    # Get beginning of day for date in timezone
    timezone.local(date.year, date.month, date.day)