Oto moja odpowiedź na to,
więc najpierw musisz przekonwertować znacznik czasu na rzeczywistą datę/czas Ruby. Jeśli otrzymasz go po prostu jako ciąg lub int z facebook, trzeba będzie zrobić coś takiego:
my_date = Time.at(timestamp_from_facebook.to_i)
Ok, więc teraz zakładając masz już swój obiekt Date ...
to_formatted_s to poręczna funkcja Ruby, która zamienia daty w sformatowane łańcuchy.
Oto kilka przykładów jego użycia:
time = Time.now # => Thu Jan 18 06:10:17 CST 2007
time.to_formatted_s(:time) # => "06:10"
time.to_s(:time) # => "06:10"
time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
time.to_formatted_s(:number) # => "20070118061017"
time.to_formatted_s(:short) # => "18 Jan 06:10"
time.to_formatted_s(:long) # => "January 18, 2007 06:10"
time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
Jak widać: : DB: liczba,: krótki ... są niestandardowe formaty daty.
Aby dodać własny format niestandardowy, można utworzyć ten plik config/inicjatorów/time_formats.rb i dodać tam własne formaty, na przykład tutaj jest jeden:
Date::DATE_FORMATS[:month_day_comma_year] = "%B %e, %Y" # January 28, 2015
Gdzie : month_day_comma_year to nazwa twojego formatu (możesz zmienić to na dowolne) i gdzie % B% e,% Y jest formatem daty unix.
Oto krótki Cheatsheet dniu wydania UNIX składni, dzięki czemu można szybko skonfigurować swoją Format niestandardowy:
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") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"
Nadzieja to pomogło ci. Zrobiłem również github gist tego małego przewodnika, na wypadek, gdyby ktoś wolał.
Nie wymyśliłem, jak z niego korzystać. Czy możesz podać mi mały przykład? – Alexandre
my_date_variable.strftime ("% m /% d /% Y") –
strftime nie jest SUCHY. Proszę nie rozpraszać strftime po swoich projektach. Przynajmniej wspomniałeś pomocnika, ale 'config/initializers/time_formats.rb' jest bardziej idiomatyczny dla Rails. – PhilT