Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 250
Blake Taylor Avatar asked Nov 02 '11 23:11

Blake Taylor


2 Answers

date = Date.today

date.to_time.in_time_zone('America/New_York').beginning_of_day

Currently outputs => 2011-11-02 00:00:00 -0400

Time.now.in_time_zone('Asia/Shanghai').beginning_of_day

Currently outputs => 2011-11-03 00:00:00 +0800

date = Date.today

date.to_time.in_time_zone('Asia/Shanghai').beginning_of_day

Currently outputs => 2011-11-02 00:00:00 +0800

like image 187
Timothy Hunkele Avatar answered Oct 14 '22 07:10

Timothy Hunkele


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)
like image 37
Blake Taylor Avatar answered Oct 14 '22 08:10

Blake Taylor