## Find the date n days later

To find the date n days later, convert n days to seconds and add them to the current time(seconds from the epoch) obtained with the time function. Convert the added seconds to date and time notation using the localtime function.

use strict; use warnings; use Time::Local 'timelocal'; print "(1) Find the date n days later\n"; # 2 days later my $n = 2; # Convert the date to seconds and add it. my $sec_from_epoch = time + (60 * 60 * 24 * $n); # Convert seconds to date and time my ($sec, $min, $hour, $mday, $month, $year) = localtime($sec_from_epoch); $month ++; $year += 1900; print "$n days later is $year year $month month $mday day $hour hour $min minutes $sec seconds.\n";

Output

(1) Find the date n days later Two days later, it is 23:02:43 on October 24, 2008.

### Explanation

#### Convert date to seconds and add

When dealing with time, convert it to seconds before handling it. You can get the number of seconds from the epoch with the time function, so add the number of seconds after n days to it.

# 2 days later my $n = 2; # Convert the date to seconds and add it. my $sec_from_epoch = time + (60 * 60 * 24 * $n);

If you want to get two days after a particular date, first use the timelocal function of the Time::Local module to find the number of seconds since the epoch on a particular date.

After that, the procedure is the same as above.

use Time::Local 'timelocal'; # March 31, 2000 my $year = 2000 - 1900; my $mon = 3 -1; my $mday = 31; my $sec_from_epoch = timelocal (0, 0, 0, $mday, $mon, $year);

#### Convert seconds to date and time

Convert seconds to date and time using the localtime function.

# Convert seconds to date and time my ($sec, $min, $hour, $mday, $month, $year) = localtime($sec_from_epoch); $month ++; $year += 1900;