簡體   English   中英

Rails為對象的created_at和Updated_at顯示錯誤的日期

[英]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-年與世紀

APIdock-日期時間

在您的代碼中使用這種格式

<%= article.updated_at.strftime("%A, %d %B %Y %l:%M%p") %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM