Ось я відповідаю на це,
тому спочатку вам потрібно буде перетворити мітку часу на фактичну дату / час Ruby. Якщо ви отримуєте його просто як рядок або int з facebook, вам потрібно буде зробити щось подібне:
my_date = Time.at(timestamp_from_facebook.to_i)
Добре, отже, припустимо, у вас вже є об’єкт дати ...
to_formatted_s - це зручна функція Ruby, яка перетворює дати у відформатовані рядки.
Ось кілька прикладів його використання:
time = Time.now
time.to_formatted_s(:time)
time.to_s(:time)
time.to_formatted_s(:db)
time.to_formatted_s(:number)
time.to_formatted_s(:short)
time.to_formatted_s(:long)
time.to_formatted_s(:long_ordinal)
time.to_formatted_s(:rfc822)
Як бачите:: db,: number,: short ... - це власні формати дат.
Щоб додати свій власний формат, ви можете створити цей файл: config / initializers / time_formats.rb і додати туди свої власні формати, наприклад ось один:
Date::DATE_FORMATS[:month_day_comma_year] = "%B %e, %Y"
Де : month_day_comma_year - це ім'я вашого формату (ви можете змінити це на все, що завгодно), а де % B% e,% Y - формат дати unix.
Ось коротка таблиця синтаксису дати Unix, щоб ви могли швидко налаштувати власний формат:
From http://linux.die.net/man/3/strftime
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%e - Day of the month without leading 0 (1..31)
%g - Year in YY (00-99)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
t = Time.now
t.strftime("Printed on %m/%d/%Y")
t.strftime("at %I:%M%p")
Сподіваюся, це вам допомогло. Я також склав суть цього невеликого посібника на випадок, якщо хтось віддасть перевагу.