[英]Rails seems to be creating objects in wrong time zone for created_at, updated_at values
[英]Rails displays wrong date for object's created_at and updated_at
我的申請書有問題。 我正在制作一個簡單的CMS,我需要顯示帖子的創建日期。 我現在在做什么無法正常工作。 它正確顯示時間,但不顯示日期。 對於每個條目,它都顯示日期為10月20日,盡管不是。
我通過rails c
檢查了我的數據庫,它存儲了正確的值。 因此,問題一定在顯示代碼中。
這是我的輸出方式:
<% @articles.each do |article| %>
<h3 class="article-title"><u><%= link_to article.title, article %></u></h3>
<p><%= article.text.html_safe %></p>
<div class="article-info">
<p>Posted on <em><%= article.updated_at.strftime("%A, %C %B %G %R") %> </em> by <em><%= article.user.username %></em> |
<%= link_to 'Comments...', article %>
<% if user_signed_in? and article.user == current_user %>
| <%= link_to 'Edit', edit_article_path(article) %>
| <%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you certain you want to delete this?' } %> </p>
<% end %>
</div>
<% if article.tag_list.any? %>
<div class="article-tags">Tags: <%= raw article.tag_list.map { |tag| link_to tag, tag_path(tag)}.join(", ") %></div>
<% end %>
<hr />
<% end %>
什么會導致這種行為?
您的問題是strftime屬性:
<%= article.updated_at.strftime("%A, %C %B %G %R") %>
%A-工作日的完整名稱(
Sunday'') %C - year / 100 (round down. 20 in 2009) %B - The full month name (
一月'')%G-基於周的年份%R-24小時時間(%H:%M)
您需要使用以下代碼:
<%= article.updated_at.strftime("%A, %d %B %Y %R") %>
%d-%d-每月的某天,零填充(01..31)
%Y-年與世紀
在您的代碼中使用這種格式
<%= article.updated_at.strftime("%A, %d %B %Y %l:%M%p") %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.