---
# System prepended metadata

title: Rails 更改時區(time_zone)
tags: [ruby, rails]

---

# Rails 更改時區(time_zone)

###### tags: `rails`, `ruby`
環境:
> ruby 2.6.7
> 
> Rails 6.1.3.1
> 
> Ubuntu 20.04 LTS

<font size=4>**資源連結:** </font>**https://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html**

## :memo:

### Step 1: 移至專案/Appname/config/application.rb

加上<font size=4>`config.time_zone = '時區'`</font>

```ruby=
require_relative "boot"
require "rails/all"
Bundler.require(*Rails.groups)

module MyMediumFull
  class Application < Rails::Application

    config.load_defaults 6.1
    config.time_zone = 'Tokyo'

  end
end
# 
```

### Step 2: 在想要的頁面(view),加上<%= story.updated_at %>

```ruby=
<% @stories.each do |story| %>
    <%= story.title %>
    <p>
        <%= story.content %>
    </p>
    <p>
        最後編集時間：<%= story.updated_at %>
    </p>
```
**顯示以下畫面:**

![](https://i.imgur.com/zIpz10h.jpg)
