LearningRoR
LearningRoR

Reputation: 27192

How do I make a today's date method based on users current Time zone?

I have a method in my class that defines what today is.

class Practice
  attr_accessible :date
  def self.today
    where(:date => Date.today)
  end
end

I got my Time.zone to be the same as the user's using a before filter on my application controller.

before_filter :set_user_time_zone

private

def set_user_time_zone
  if signed_in?
    Time.zone = current_user.time_zone
  end
end

But when it comes to Date.today it see's it as the US EAST even If put the user's time zone in Tokyo. So its Aug 19th in the USA and over their it's Aug 20th. How can I make it be Aug 20th instead or the Time zone's date?

Upvotes: 1

Views: 72

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96914

Instead of changing Time.zone, you could use in_time_zone:

Time.current.in_time_zone(current_user.time_zone).to_date

You'll likely have to pass the time zone in as a method argument since current_user isn't typically available in models.

Upvotes: 4

Related Questions