7.21. Formatting and Printing Date/Time Values

You can obtain the canonical representation of the date and time by calling the asctime method (“ASCII time”); it has an alias called ctime, for those who already know it by that name.

You can obtain a similar result by calling the to_s method. This is the same as the result you would get if doing a simple puts of a date/time value.

The strftime method of class Time formats a date and time in almost any form you can think of. Other examples in this chapter have shown the use of the directives %a, %A, %U, %W, %H, %M, %S, %I, and %p; we list here all the remaining directives that strftime recognizes:

%b Abbreviated month name ("Jan") %B Full month name ("January") %c Preferred local date/time representation ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.