Search code examples
perl

How to get the date difference in seconds ,in perl


I have two dates , (they are datetime objects).

How can i get the difference between these two dates, in seconds ?

my $givenDate1 =  DateTime->new(
      year       => 1982,
      month      => 10,
      day        => 25,
      hour       => 7,
      minute     => 19,
      second     => 35,

      time_zone  => 'UTC',
  );
my $givenDate2 =  DateTime->new(
      year       => 1982,
      month      => 10,
      day        => 25,
      hour       => 7,
      minute     => 20,
      second     => 50,

      time_zone  => 'UTC',
  );

It should return the time difference as 75 secs


Solution

  • say $givenDate2->epoch - $givenDate1->epoch;