Relative Content

Tag Archive for rubytimestrptime

Ruby strptime beginning and end of string

Is it possible to parse a string with Ruby Date.strptime that constrains the beginning and end of the string? What I’m looking for is a more strict way to parse strings as dates that will throw an error if a string doesn’t match exactly. For example, with the string "SEPTEMBER 8, 2016", if I use Date.strptime(str, "%B %d, %y"), it will return the date Tue, 08 Sep 2020, when the year should be 2016. Is there a way to constrain strptime so that it has to match the entire string, like with ^ and $ in regexes? I want it to raise an error with these sorts of strings rather than returning the wrong result.