Perl Scripts ·

Dates in Perl: Today, Timestamps, Adding Days and Age

By Manu Mathew, Technical Lead Engineer · Published 17 February 2016 · Last updated 5 October 2026

Isometric illustration of a calendar and an hourglass, with a blue arrow stepping forward across days

Date code is easy to get almost right. A report shows the wrong day near midnight, or an age is a year out the day before a birthday. This page brings together several short notes on dates in Perl I wrote between 2013 and 2016. I have rewritten them in one place, with tested examples and current advice, so you don’t have to piece the answer together from fragments. The examples use POSIX and Time::Piece, which both ship with Perl, so there is nothing to install.

Quick answer

strftime('%d/%m/%Y', localtime) from POSIX formats today’s date, and time returns the Unix timestamp. With Time::Piece, add or subtract days as $t + 7 * ONE_DAY, subtract two dates to get a Time::Seconds object, and compare month and day to work out whether a birthday has passed.

I tested each example with Perl 5.40. Outputs marked “e.g.” depend on the day you run them.

How do I get today’s date in a given format?

strftime from POSIX formats localtime with the codes you choose.

use strict;
use warnings;
use POSIX qw(strftime);

print strftime('%m/%d/%Y', localtime), "\n";    # e.g. 10/04/2026 (MM/DD/YYYY)
print strftime('%d/%m/%Y', localtime), "\n";    # e.g. 04/10/2026 (DD/MM/YYYY)
print strftime('%Y-%m-%d', localtime), "\n";    # e.g. 2026-10-04 (ISO 8601)

%d is the day, %m the month, %Y the four-digit year. Prefer the ISO form, %Y-%m-%d, for anything stored or sorted, since it sorts correctly as text.

How do I get the current Unix timestamp?

time returns the number of seconds since 1970-01-01 00:00:00 UTC.

use strict;
use warnings;

my $now = time;                                 # seconds since 1970-01-01 00:00:00 UTC
print "$now\n";                                 # e.g. 1791131124
print scalar(localtime($now)), "\n";            # e.g. Sun Oct  4 21:55:24 2026

How do I add or subtract days from today’s date?

With Time::Piece, add or subtract multiples of ONE_DAY from a date object.

use strict;
use warnings;
use Time::Piece;
use Time::Seconds qw(ONE_DAY);

my $today     = localtime;                      # a Time::Piece object
my $last_week = $today - 7 * ONE_DAY;
my $next_week = $today + 7 * ONE_DAY;

print 'Today:        ', $today->strftime('%d/%m/%Y'), "\n";
print 'Minus 7 days: ', $last_week->strftime('%d/%m/%Y'), "\n";
print 'Plus 7 days:  ', $next_week->strftime('%d/%m/%Y'), "\n";

The older version of this note did the same with time - 7 * 24 * 60 * 60 and strftime, which also works. Either way, a day is treated as exactly 86,400 seconds, so around a daylight-saving change the time of day can shift by an hour. India has no daylight saving, but servers in other zones do. If only the calendar date matters, work at midday UTC:

use strict;
use warnings;
use Time::Piece;
use Time::Seconds qw(ONE_DAY);

# Work with dates at noon UTC, so a daylight-saving change can't push you into the wrong day
my $start = Time::Piece->strptime('2026-03-27 12:00', '%Y-%m-%d %H:%M');
my $later = $start + 3 * ONE_DAY;
print $later->ymd, "\n";                        # 2026-03-30

How do I find the difference between two dates?

Subtract one Time::Piece from another. The result is a Time::Seconds object with seconds, hours and days methods.

Isometric illustration of two calendar pages joined by a blue bar measuring the days between them
use strict;
use warnings;
use Time::Piece;

my $start = Time::Piece->strptime('2015-06-12', '%Y-%m-%d');
my $end   = Time::Piece->strptime('2015-07-13', '%Y-%m-%d');

my $diff = $end - $start;                       # a Time::Seconds object
printf "Seconds: %d\n", $diff->seconds;         # Seconds: 2678400
printf "Hours:   %d\n", $diff->hours;           # Hours:   744
printf "Days:    %d\n", $diff->days;            # Days:    31

Months and years have no fixed length, so Time::Seconds’ months and years methods use averages. For calendar months, count them as in the age example below.

How do I calculate someone’s age in years?

Subtract the birth year from the current year, then take one off if this year’s birthday hasn’t come yet.

use strict;
use warnings;
use Time::Piece;

sub age_in_years {
    my ($birth, $on) = @_;                      # two Time::Piece objects
    my $age = $on->year - $birth->year;
    # Not had this year's birthday yet? Then take one off.
    $age-- if sprintf('%02d%02d', $on->mon, $on->mday) lt sprintf('%02d%02d', $birth->mon, $birth->mday);
    return $age;
}

my $birth = Time::Piece->strptime('1990-04-06', '%Y-%m-%d');
my $on    = Time::Piece->strptime('2026-04-05', '%Y-%m-%d');
print age_in_years($birth, $on), "\n";          # 35 (the birthday is a day away)
print age_in_years($birth, Time::Piece->strptime('2026-04-06', '%Y-%m-%d')), "\n";   # 36

Comparing MMDD strings is a simple, reliable way to check whether the birthday has passed. It also handles 29 February: in a non-leap year the birthday counts from 1 March.

How do I show an age in days, weeks, months or years?

Count whole months between the two dates, then pick the unit from a few thresholds, as some medical and childcare systems do.

use strict;
use warnings;
use Time::Piece;
use Time::Seconds qw(ONE_DAY);

# Whole months between two dates (counting a month only once its day is reached)
sub months_between {
    my ($from, $to) = @_;
    my $months = ($to->year - $from->year) * 12 + ($to->mon - $from->mon);
    $months-- if $to->mday < $from->mday;
    return $months;
}

# Age shown in days, weeks, months or years depending on how young the person is
sub age_label {
    my ($birth, $on) = @_;
    my $days   = int(($on - $birth) / ONE_DAY);
    my $months = months_between($birth, $on);
    my $years  = int($months / 12);

    return "$days days"                    if $days < 28;
    return int($days / 7) . ' weeks'       if $months < 4;
    return "$months months"                if $months < 24;
    return "$years years " . ($months % 12) . ' months' if $years < 18;
    return "$years years";
}

my $on = Time::Piece->strptime('2026-10-04', '%Y-%m-%d');
for my $birth ('2026-09-20', '2026-08-01', '2025-11-15', '2019-01-10', '1990-04-06') {
    my $b = Time::Piece->strptime($birth, '%Y-%m-%d');
    printf "%s: %s\n", $birth, age_label($b, $on);
}
# 2026-09-20: 14 days
# 2026-08-01: 9 weeks
# 2025-11-15: 10 months
# 2019-01-10: 7 years 8 months
# 1990-04-06: 36 years

The thresholds here (under 28 days in days, under 4 months in weeks, under 2 years in months, under 18 years in years and months) follow an older version of this note. Check the rules your organisation uses before relying on them.

Need help? If your Perl date handling is part of an older application that needs fixing or updating, I can take a look. See Perl development, or email manu@manu.co.in.

Frequently asked questions

Should I use Time::Piece or DateTime?

Time::Piece ships with Perl and is enough for formatting, parsing and adding days in local time or UTC. DateTime, from CPAN, handles named time zones, daylight saving rules and calendar maths more fully. Use Time::Piece for simple scripts and DateTime when time zones matter.

Why does adding 24 hours sometimes give the wrong date?

In zones with daylight saving, some days last 23 or 25 hours, so adding 86,400 seconds can land on the wrong day or hour. India has no daylight saving, so IST is not affected, but servers set to other zones are. Use DateTime’s add(days => 1) when it matters.

How do I parse a date string such as 24/09/2013?

Use Time::Piece->strptime('24/09/2013', '%d/%m/%Y'). It returns a Time::Piece object you can format, compare or add days to. It dies when the string doesn’t match the format, so wrap it in eval when the date comes from users.

How do I get the day of the week for a date?

With Time::Piece, $t->day_of_week returns 0 for Sunday to 6 for Saturday, and $t->fullday returns the name, such as “Tuesday”. With POSIX, strftime('%A', localtime) gives the full name of today’s day in the same way.

Related notes: MySQL Unix timestamps and time zones; Perl files, folders, CSV and downloads; random numbers in Perl; hiring a remote Perl developer.

Need help with Perl code?

If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.

You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.

Sources: POSIX: strftime; perlfunc: time; Time::Piece; Time::Seconds; perlfaq4: data, dates.

Back to the blog