Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6100671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:24:49+00:00 2026-05-23T13:24:49+00:00

This code finds the difference between today and a fixed date. #!/usr/bin/perl use strict;

  • 0

This code finds the difference between today and a fixed date.

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

use DateTime ();
use DateTime::Duration ();
use DateTime::Format::Strptime ();

my $date = "23/05-2022";

my $parser = DateTime::Format::Strptime->new(
    pattern     => '%d/%m-%Y',
    time_zone   => 'local',
    );

$date = $parser->parse_datetime($date);

my $today = DateTime->today(time_zone=>'local');

my $d = DateTime::Duration->new($today - $date);

print Dumper $d->delta_days;

The problem is that is only outputs -22 days.

If I do print Dumper $d; I can see the -130 months as well.

$VAR1 = bless( {
                 'seconds' => 0,
                 'minutes' => 0,
                 'end_of_month' => 'preserve',
                 'nanoseconds' => 0,
                 'days' => -22,
                 'months' => -130
               }, 'DateTime::Duration' );

How do I get it to output the result in days?

Doing

print Dumper $d->delta_days + $d->delta_months*30;

doesn’t seam like an elegant solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:24:49+00:00Added an answer on May 23, 2026 at 1:24 pm

    At first you need to do the correct subtraction. There exists delta_md, delta_days, delta_ms and subtract_datetime_absolute. Depending on which unit you later want, you need to pick the right subtraction. The problem is that not every unit is convertible later without time_zone information. Thats the reason why you need to pick the correct delta method.

    For example a day can have 23 Hours or 24 or 25 Hours, depending on the time zone. Because of that, you need to specify how the subtraction should work. Because you want the days later, the subtraction need to focus on days, rather focus on hours. Don’t use the overload feature, because it only does a best fit.

    That means you need to do a delta_days subtraction.

    my $dur = $date->delta_days($today);
    

    Now $dur is a DateTime::Duration object. You need to knew that it always tries to best fit the days, weeks, years, months if possible. That means your days will split in weeks and days. Because this conversion is always a constant.

    If you don’t want this “best fit” you need to call the in_units method and convert it only to days.

    my $days = $dur->in_units('days');
    

    But like i said before in_units only can do a conversion where it is possible. A call with in_units('hours') will not work on this object and just return a zero because you cant convert days to hours. If you want hours for example, you need to do a delta_ms, and on this object you can call in_units('hours')

    The complete example:

    #!/usr/bin/env perl
    use 5.010;
    use strict;
    use warnings;
    
    use DateTime;
    use DateTime::Format::Strptime;
    
    my $date = "23/05-2022";
    my $parser = DateTime::Format::Strptime->new(
        pattern     => '%d/%m-%Y',
        time_zone   => 'local',
    );
    $date = $parser->parse_datetime($date);
    
    my $today = DateTime->new(
        day   => 1,
        month => 7,
        year  => 2011,
        time_zone => 'local'
    );  
    
    my $dur = $date->delta_days($today);
    say "Weeks:          ", $dur->weeks;
    say "Days:           ", $dur->days;
    say "Absolute Days:  ", $dur->in_units('days');
    say "Absolute Hours: ", $date->delta_ms($today)->in_units('hours');
    

    The output of this program is:

    Weeks:          568
    Days:           3
    Absolute Days:  3979
    Absolute Hours: 95496
    

    And just for info:
    1) You don’t need to load DateTime::Duration its get loaded with DateTime.
    2) You dont need (). These modules are OOP and don’t export/import anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter
I frequently find myself writing code like this: List<int> list = new List<int> {
I find this line in the ZenTest source code: result = @test_mappings.find { |file_re,
I find myself writing code that looks like this a lot: set<int> affected_items; while
I often find I want to write code something like this in C#, but
I came across this line in some code and can't find the syntax defined
I'm refactoring some code I inherited from a long-gone developer, and I find this:
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.