Hi Guys How to convert number of days to number of year and month in Perl.

Recommended Answers

All 2 Replies

I like to use Date::Manip, but it is rather large.

http://search.cpan.org/~sbeck/Date-Manip-6.11/lib/Date/Manip.pod

You can do a TON of stuff with this module. Run the pod to text to get the full help text with formats, date calculations, etc.

use Date::Manip;
$ENV{TZ}="EST";
use strict;
use warnings;
my $ptoday=ParseDate("today");
my $ftoday=UnixDate($ptoday,"%m/%d/%Y");
print "$ftoday\n";
my $month=UnixDate($ptoday,"%m");
my $day=UnixDate($ptoday,"%d");
my $year=UnixDate($ptoday,"%Y");

my $dayofyear=Date_DayOfYear($month,$day,$year);
print "$dayofyear\n";

Here's a little additional code for what you want...

use Date::Manip;
$ENV{TZ}="EST";
use strict;
use warnings;
my $ptoday=ParseDate("today");
my $ftoday=UnixDate($ptoday,"%m/%d/%Y");
print "$ftoday\n";
my $month=UnixDate($ptoday,"%m");
my $day=UnixDate($ptoday,"%d");
my $year=UnixDate($ptoday,"%Y");

my $dayofyear=Date_DayOfYear($month,$day,$year);
print "$dayofyear\n";

my $ndate=ParseDate("this $dayofyear day of this year");
my $fdate=UnixDate($ptoday,"%m/%d");
print "$fdate\n";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.