Howdy ya’ll. As much as we’d prefer to just deal with nicely formatted data; the
real world sometimes requires that we parse weird datetime strings into actual
DateTime objects. Today’s article is all about how to parse dates and times from
arbitrary strings using the powerful method
DateTime.strptime we can turn crazy data like “07-FEB-12
03.39.14.490227” into actual DateTime objects. Seriously. It’s actually
possible, and we won’t even need to break out the ol’ regular expressions. Let’s
look at the basics.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
See that? We got the same DateTime object using strptime that we got using parse. Right on! But what the heck did we just do?
The strptime method takes two arguments: the date/time string to parse, and the date format pattern. The date format pattern is a set of rules for describing all the different pieces of a date/time that we could possibly find within the string representation of a date.
The date format pieces are standard bits of computing lore. I never remember
what they are and I always confuse
%m (the month as a number, e.g. 2) and
(the minute of the hour, e.g. 2). Oh dates, you’re so ambiguous. Ruby Dock has a great
time formatter reference
as part of the strftime method reference.
In the example above, we use:
- %Y : the four digit year
- %m : the month as an integer with a leading zero
- %d : the day of the month as an integer with a leading zero
- %H : the hour of the day (24 hour style, 0-23)
- %M : the minute of the hour with leading zero
- %S : the second of the minute with leading zero
The real trick to parsing with strptime is just knowing that there is a date/time formatter for just about any date string.
So let’s tackle our challenge: 07-FEB-12 03.39.14.490227
What is that? .490227? Ugh. Let’s try throwing .parse at it.
.parse was able to puzzle out the date; but not the time. Time to dig
through the formatting docs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
Alright! An actual DateTime object that looks like it’s gotten at least pretty close to the intention of this weird date/time string.
Parsing date/time strings into structured DateTime objects is one of those tedious tasks. It’s always a little frustrating to go through this process because we know that in almost all cases the string we are working hard to put into a structured DateTime came out of a structured DateTime object somewhere in its history. Thankfully Ruby provides us with some good functions to make it as easy as possible.