Ruby Time and date usage detailed

Source: Internet
Author: User
Tags api manual current time local time time and date



Web applications, and time to deal with is not exempt. Rails has extended the Ruby time module. This article was made on January 29, 2011, and the ruby version is 1.8.7, and the rails version is 3.0.3





Basic Time Conversion
>> Now=time.now
=> Sat 29 21:47:07 0800 2011
#utc seconds convert to each other
>> now.to_i
=> 1296308827
>> time.at (1296308827)
=> Sat 29 21:47:07 0800 2011
# some variables of the current time
>> now.sec
=> 7
>> Now.min
=> 47
>> Now.hour
=> 21
>> Now.month
=> 1
>> Now.year
=> 2011
#这是星期几(NOTE!!! Sunday is return 0)
>> Now.wday
=> 6
#now is the first few days of the month
>> Now.day
=> 29
#now is the first few days of the year
>> Now.yday
=> 29
Array of #time parameters
>> now.to_a
=> [7, 1, 6, O, False, CST]





Time output
>> now.strftime ("%y-%m-%d%h:%m:%s")
=> "2011-01-29 21:47:07"





The parameters are explained as follows





%a-English abbreviation for day of the week (' Sun ')
%A-The English full name of the week (' Sunday ')
%b-English shorthand for month
%B-The English full name of the month (' January ')
%c-Default preferred local time output format
%d-Days of the month (01..31)
Hours of%h-24 hours (00..23)
Hours of%i-12 hours (01..12)
%j-the first day of the Year (001..366)
%m-month (01..12)
%m-min (00..59)
%p-morning or afternoon (' AM ' or ' PM ')
%s-Number of seconds (00..60)
%u-The first weeks of the year starting from Sunday (00..53)
%w-The first weeks of the year starting from Monday (00..53)
%w-Now is the day of the week (Sunday is 0, 0..6)
%x-Default Date output format ("01/29/11")
%x-Default time output format ("21:47:07")
%y-the latter two digits of the year (00..99)
%Y-year
%Z-time Zone name
%%-output% character





This is the basic method of Ruby, and rails does more to extend it





#Rewrite the to_s method, able to accept parameters
>> now.to_s
=> "Sat 29 21:47:07 +0800 2011"
>> now.to_s (:d b)
=> "2011-01-29 21:47:07"
>> now.to_s (: number)
=> "20110129214707"
>> now.to_s (: Time)
=> "21:47"
>> now.to_s (: short)
=> "21:47"
>> now.to_s (: Long)
=> "January 29, 2011 21:47"
>> now.to_s (: long_ordinal)
=> "January 29th, 2011 21:47"
>> now.to_s (: rfc822)
=> "Sat, 2011 21:47:07 +0800"





If you want to design your own time output format, click the following method to create a new configuration file





# CONFIG/INITIALIZERS/TIME_FORMATS.RB
Time::D ate_formats[:month_and_year] = "%B%Y"
Time::D ate_formats[:short_ordinal] = lambda {|time| time.strftime ("%B #{time.day.ordinalize}")}





Rails some extensions to dates





#specified time
>> Now.change (: year=>2012,: month=>12,:d ay =>,: Hour => 0,: Min => 0,: Sec => 0,: usec => 0 )
=> Fri Dec 21 00:00:00 0800 2012



#Begginning family
>> Now.beginning_of_day
=> Sat 29 00:00:00 0800 2011
>> Now.midnight
=> Sat 29 00:00:00 0800 2011
>> Now.beginning_of_week
=> Mon 24 00:00:00 0800 2011
>> Now.beginning_of_month
=> Sat 01 00:00:00 0800 2011
>> Now.beginning_of_quarter
=> Sat 01 00:00:00 0800 2011
>> Now.beginning_of_year
=> Sat 01 00:00:00 0800 2011
#End family
>> Now.end_of_day
=> Sat 29 23:59:59 0800 2011
>> Now.end_of_week
=> Sun 30 23:59:59 0800 2011
>> Now.end_of_month
=> Mon 31 23:59:59 0800 2011
>> Now.end_of_quarter
=> Thu Mar 31 23:59:59 0800 2011
>> Now.end_of_year
=> Sat Dec 31 23:59:59 0800 2011
#Magic method of time
>> Now.yesterday
=> Fri 28 21:47:07 0800 2011
>> Now.tomorrow
=> Sun 30 21:47:07 0800 2011
>> Now.next_week
=> Mon 31 00:00:00 0800 2011
>> Now.next_month
=> Mon Feb 28 21:47:07 0800 2011
>> Now.next_year
#Note that there is no prev_week
>> Now.prev_month
=> Wed Dec 29 21:47:07 0800 2010
>> Now.prev_year
=> Fri 29 21:47:07 0800 2010
#The number of seconds passed today
>> Now.seconds_since_midnight
=> 78427.615017
#dateoutput
>> now.to_date
=> Sat, 2011
>> Now.to_datetime
=> Sat, 2011 21:47:07 0800
#calculated by seconds
>> Now.ago (3600)
=> Sat 29 20:47:07 0800 2011
>> now.since (3600)
=> Sat 29 22:47:07 0800 2011



There are actually many other methods that are not listed, whichever is the use of the Rails API manual.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.