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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With