Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format the date using Ruby on Rails

The flickr api provides a posted date as unix timestamp one: "The posted date is always passed around as a unix timestamp, which is an unsigned integer specifying the number of seconds since Jan 1st 1970 GMT."

For example, here is the date '1100897479'. How do I format it using Ruby on Rails?

like image 730
Alexandre Avatar asked Jun 18 '12 20:06

Alexandre


People also ask

What is Strftime Ruby?

Ruby | Time strftime() function Time#strftime() is a Time class method which returns the time format according to the directives in the given format string. Syntax: Time.strftime() Parameter: Time values. Return: time format according to the directives in the given format string.


2 Answers

Once you have parsed the timestamp string and have a time object (see other answers for details), you can use Time.to_formatted_s from Rails. It has several formats built in that you can specify with symbols.

Quote:

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" 

(Time.to_s is an alias)

You can also define your own formats - usually in an initializer (Thanks to Dave Newton for pointing this out). This is how it's done:

# config/initializers/time_formats.rb Time::DATE_FORMATS[:month_and_year] = "%B %Y" Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") } 
like image 57
CMW Avatar answered Oct 13 '22 05:10

CMW


Here's my go at answering this,

so first you will need to convert the timestamp to an actual Ruby Date/Time. If you receive it just as a string or int from facebook, you will need to do something like this:

my_date = Time.at(timestamp_from_facebook.to_i) 

OK, so now assuming you already have your date object...

to_formatted_s is a handy Ruby function that turns dates into formatted strings.

Here are some examples of its usage:

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" 

As you can see: :db, :number, :short ... are custom date formats.

To add your own custom format, you can create this file: config/initializers/time_formats.rb and add your own formats there, for example here's one:

Date::DATE_FORMATS[:month_day_comma_year] = "%B %e, %Y" # January 28, 2015 

Where :month_day_comma_year is your format's name (you can change this to anything you want), and where %B %e, %Y is unix date format.

Here's a quick cheatsheet on unix date syntax, so you can quickly setup your custom format:

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" 

Hope this helped you. I've also made a github gist of this little guide, in case anyone prefers.

like image 22
Edu Wass Avatar answered Oct 13 '22 04:10

Edu Wass