Ruby supports time formatting and parsing via pattern-based layouts.
When you run this program, you’ll see output similar to:
In Ruby, we use the Time class for working with dates and times. The strftime method is used for formatting times, while parse and strptime are used for parsing strings into Time objects.
Ruby’s time formatting is more flexible than some other languages, as it doesn’t require a specific reference time. Instead, it uses format specifiers like %Y for year, %m for month, etc.
For parsing, Ruby provides both Time.parse, which can handle many common formats automatically, and Time.strptime, which allows you to specify the exact format of the input string.
When parsing fails, Ruby raises an ArgumentError with a message explaining the problem, rather than returning an error value.