simbatish
simbatish

Reputation: 123

Perl / regex date format convert

I am trying to convert a string in dateformat such as 'Aug 12/11'.. to 'YYYY-MM-DD', which in this example would be '2011-08-12'.

What would be the best perl/regex to use for this conversion? i was able to do it by parsing each and converting it manually.. but i am guessing there is a quicker way to do it with perl/regex. Thanks.

Upvotes: 5

Views: 798

Answers (2)

brainbuz
brainbuz

Reputation: 444

Time::Piece has been a core module for a long time, it does have the ability to parse a string into a Time::Piece object, which it can then format for you in 10 gazillion different formats. If Time::Piece isn't installed you could try looking for some of the other hundreds of date time modules that you can see listed on cpan. (use a one liner: perl -e "use Module::IHope::IsHere || die \'Not found\'").

`Time::Piece->strptime(STRING, FORMAT)
                        # see strptime man page. Creates a new
                        # Time::Piece object`

Failing that I would start installing modules in my home directory.

Upvotes: 2

Dave Goodell
Dave Goodell

Reputation: 2153

I would recommend against doing any parsing of dates like this yourself, especially by trying to come up with one or two regexes. There are lots of existing perl modules to do this, such as Date::Manip. Your many options are reasonably covered in this article.

Upvotes: 4

Related Questions