DateTime::Duration - Duration objects for date math


DateTime::Duration - Duration objects for date math


  use DateTime::Duration;
  $d = DateTime::Duration->new( years   => 3,
                                months  => 5,
                                weeks   => 1,
                                days    => 1,
                                hours   => 6,
                                minutes => 15,
                                seconds => 45, 
                                nanoseconds => 12000 );
  # Convert to different units
  $d->in_units('days', 'hours', 'seconds');
  # The important parts for date math
  my %deltas = $d->deltas
  print $d->end_of_month_mode;
  # Multiple all deltas by -1
  my $opposite = $d->inverse;
  my $bigger  = $dur1 + $dur2;
  my $smaller = $dur1 - $dur2; # the result could be negative
  my $bigger  = $dur1 * 3;
  my $base_dt = DateTime->new( year => 2000 );
  my @sorted =
      sort { DateTime::Duration->compare( $a, $b, $base_dt ) } @durations;
  # Human-readable accessors, always positive, but use
  # DateTime::Format::Duration instead
  if ( $d->is_positive ) { ... }
  if ( $d->is_zero )     { ... }
  if ( $d->is_negative ) { ... }


This is a simple class for representing duration objects. These objects are used whenever you do date math with

See the How Date Math is Done section of the documentation for more details. The short course: One cannot in general convert between seconds, minutes, days, and months, so this class will never do so. Instead, create the duration with the desired units to begin with, for example by calling the appropriate subtraction/delta method on a object.


Like DateTime itself, DateTime::Duration returns the object from mutator methods in order to make method chaining possible.

DateTime::Duration has the following methods:


This class overloads addition, subtraction, and mutiplication.

Comparison is not overloaded. If you attempt to compare durations using <=> or cmp, then an exception will be thrown! Use the compare() class method instead.


Support for this module is provided via the email list. See for more details.


Dave Rolsky <>

However, please see the CREDITS file for more details on who I really stole all the code from.


Copyright (c) 2003-2006 David Rolsky. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Portions of the code in this distribution are derived from other works. Please see the CREDITS file for more details.

The full text of the license can be found in the LICENSE file included with this module.

SEE ALSO mailing list

 DateTime::Duration - Duration objects for date math