Ruby 2 Time Class

The Time class in Ruby has a powerful formatting function which can help you represent the time in a variety of ways. The Time class contains Ruby's interface to the set of time libraries written in C. Time zero for Ruby is the first second GMT of January 1, 1970. Ruby's DateTime class is superior to Time for astronomical and historical applications, but you can use Time for most everyday programs.
The  strftime function is modelled after C's printf. The p042time.rb program shows some of these functions.
  1. t = Time.now  
  2. # to get day, month and year with century  
  3. # also hour, minute and second  
  4. puts t.strftime("%d/%m/%Y %H:%M:%S")  
  5.   
  6. # You can use the upper case A and B to get the full  
  7. # name of the weekday and month, respectively  
  8. puts t.strftime("%A")  
  9. puts t.strftime("%B")  
  10.   
  11. # You can use the lower case a and b to get the abbreviated  
  12. # name of the weekday and month, respectively  
  13. puts t.strftime("%a")  
  14. puts t.strftime("%b")  
  15.   
  16. # 24 hour clock and Time zone name  
  17. puts t.strftime("at %H:%M %Z")  
The output is:
  1. >ruby p042time.rb  
  2. 10/09/2006 10:06:31  
  3. Sunday  
  4. September  
  5. Sun  
  6. Sep  
  7. at 10:06 India Standard Time  
  8. >Exit code: 0