What is the best way to compare dates in Perl?
perl
Solution
One of the most popular date modules is DateTime which will handle all of the corner cases and other issues surrounding date math.
This link is a FAQ for DateTime which may help you get started:
- http://datetime.perl.org/wiki/datetime/page/FAQ
The way the `DateTime` module works is that you convert your dates (which will presumably be strings) into `DateTime` objects, which you can then compare using one of several `DateTime` methods.
In the examples below, `$dt1` and `$dt2` are `DateTime` objects.
`$days` is the delta between the two dates:
my $days = $dt1->delta_days($dt2)->delta_days;
`$cmp` is -1, 0 or 1, depending on whether `$dt1` is less than, equal to, or more than `$dt2`.
my $cmp = DateTime->compare($dt1, $dt2);
Problem
I need to read 2 dates and compare them. One date is the current_date (year,month,date) the other is determined by the business logic. I then need to compare the 2 dates and see if one is before the other. How can I do the same in Perl? I am searching for good documentation, but I am finding so many Date modules in Perl. Don't know which one is appropriate for it.