簡體   English   中英

如何將非紅寶石日期轉換為可讀格式?

[英]How do I convert a non ruby on rails date into a readable format?

無論如何,在軌道上是否有紅寶石將日期2014-08-13T22:11:18.138Z轉換為更具可讀性的格式,例如dd / mm / yyyy或2014年1月1日。

它來自parse.com數據庫的createdAt字段。

我該如何轉換呢?

我需要某種正則表達式嗎?

有內置的紅寶石代碼可用於我進行轉換嗎?

您可以這樣做:

// it will be parsed to the datetime in the timezone configured by rails app.
Time.zone.parse('2014-08-13T22:11:18.138Z').strftime('%d/%m/%Y')

Date類具有許多針對標准格式的內置轉換。 確定格式(在本例中為RFC3339),您將能夠找到以下內容:

require 'date'
return Date.rfc3339('2014-08-13T22:11:18.138Z')
 #<Date: 2014-08-13 ((2456883j,0s,0n),+0s,2299161j)>

在您的語言環境中

date:
    formats:
      # Use the strftime parameters for formats.
      # When no format has been given, it uses default.
      # You can provide other formats here if you like!
      default: "%d/%m/%Y"
      short: "%b %d"
      long: "%B %d, %Y"

在你看來

<%= I18n.l @entry.created_at, :format => :short %>

暫無
暫無

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

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