Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 417
simbatish Avatar asked Aug 12 '11 22:08

simbatish


2 Answers

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.

like image 169
Dave Goodell Avatar answered Nov 15 '22 04:11

Dave Goodell


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.

like image 28
brainbuz Avatar answered Nov 15 '22 03:11

brainbuz