Thursday, May 02, 2013

Date Time Formatting in Ruby and Ruby on Rails


By on Thursday, May 02, 2013

In develop any Ruby on Rails application, most of time we need to time calculation and different types of date time. Bellow i describe different types ruby date time format and rails date time format.

So open your rails console and test this bellow examples -

Rails Date Time helpers.



now = Time.now        #=> 2013-04-30 00:14:38 +0600
now + 10.seconds      #=> 2013-04-30 00:14:48 +0600
now + 10.minutes      #=> 2013-04-30 00:24:38 +0600
now + 1.hours         #=> 2013-04-30 01:14:38 +0600
now + 1.days          #=> 2013-05-01 00:14:38 +0600
now + 1.weeks         #=> 2013-05-07 00:14:38 +0600
now + 1.months        #=> 2013-05-30 00:14:38 +0600
now + 1.years         #=> 2014-04-30 00:14:38 +0600
now - 1.years         #=> 2012-04-30 00:14:38 +0600

Rails Date Time Calculations.


now.beginning_of_day   #=> 2013-04-30 00:00:00 +0600
now.beginning_of_week  #=>2013-04-29 00:00:00 +0600
now.beginning_of_month #=>2013-04-01 00:00:00 +0600
now.yesterday          #=>2013-04-29 00:14:38 +0600
now.prev_month         #=> 2013-03-30 00:14:38 +0600
now.prev_year          #=> 2012-04-30 00:14:38 +0600
now.end_of_day         #=>2013-04-30 23:59:59 +0600
now.end_of_week        #=> 2013-05-05 23:59:59 +0600
now.end_of_month       #=> 2013-04-30 23:59:59 +0600
now.end_of_year        #=> 2013-12-31 23:59:59 +0600
now.tomorrow           #=>2013-05-01 00:14:38 +0600
now.next_month         #=>2013-05-30 00:14:38 +0600
now.next_year          #=>2014-04-30 00:14:38 +0600

Ruby Date Time Formatting.


now = Time.now                          #=>2013-04-30 00:38:26 +0600
now.strftime("%m/%d/%Y")                #=> "04/30/2013"
now.strftime("%I:%M%p")                 #=> "12:38AM"
now.strftime("%Y-%d-%m")                #=>"2013-30-04"
now.strftime("%Y-%m-%d")                #=>"2013-04-30"
now.strftime("%y-%m-%d")                #=>"13-04-30"
now.strftime("%d %B %Y")                #=>"30 April 2013"
now.strftime("%d %b %Y")                #=>"30 Apr 2013"
now.strftime("%A, %d %b %Y")            #=>"Tuesday, 30 Apr 2013"
now.strftime("%a %d %b %Y")             #=>"Tue 30 Apr 2013"
now.strftime("%Y-%m-%d %H:%M:%S %p")    #=>"2013-04-30 00:38:26 AM"
now.strftime("%Y-%m-%d %H:%M:%S %p %z") #=>"2013-04-30 00:38:26 AM +0600"

Rails Date Time Formatting.


now = Time.now          #=>2013-04-30 00:58:05 +0600
now.to_s(:db)           #=> "2013-04-30 00:58:05"
now.to_s(:number)       #=> "20130430005805"
now.to_s(:time)         #=> "00:58"
now.to_s(:short)        #=> "30 Apr 00:58"
now.to_s(:long)         #=> "April 30, 2013 00:58"
now.to_s(:long_ordinal) #=>"April 30th, 2013 00:58"


12 comments:

  1. Nice one, thanks for sharing. If you are looking for Ruby on Rails Developers in india email us at: info@cryptextechnologies.com

    ReplyDelete
  2. Very useful information shared. If you are looking for Ruby on Rails in India, then Cryptex Technologies is well-know Ruby on Rails development company in India. For any queries contact us at: info@cryptextextechnologies.com

    ReplyDelete
  3. The article provided by you is very nice and it is very helpful to know the more information.keep update with your blogs .I found a article related to you..once you can check it out.Ruby on Rails Online Course Banglore

    ReplyDelete
  4. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best. ROR Development Company India,

    ReplyDelete
  5. Thanks for sharing this blog post,Nice written skill selenium Online Course

    ReplyDelete
  6. Thanks for sharing......

    Real Trainings provide all IT-Training Course information in Hyderabad, Bangalore, Chennai . Here students can Compare all Courses with all detailed information.

    ReplyDelete
  7. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Ruby on Rails Online Course

    ReplyDelete
  8. You guys are doing the best, helping all by posting this code of Date and time. I don't know much about Ruby Development, so i hire Ruby on Rails Development Company to built my business website. Thanks for the information.

    ReplyDelete
  9. Very useful information shared. If you are looking for ruby certification

    ReplyDelete
  10. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up ruby on rails online training hyderabad

    ReplyDelete