function [ nday, nhour, nminute, nsecond, ierror ] = ymdhms_dif_dhms ( ...
  y1, m1, d1, h1, n1, s1, y2, m2, d2, h2, n2, s2 )

%*****************************************************************************80
%
%% YMDHMS_DIF_DHMS computes the DHMS difference between two YMDHMS dates.
%
%  Discussion:
%
%    The result is reported in days, minutes, hours and seconds.
%    The result is POSITIVE if the second date is later than the first.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 April 2013
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer Y1, M1, D1, H1, N1, S1, the first date.
%
%    integer Y2, M2, D2, H2, N2, S2, the second date.
%
%  Output:
%
%    integer NDAY, NHOUR, NMINUTE, NSECOND,
%    the number of days, hours, minutes, seconds between the two dates.
%
%    integer IERROR, is 1 if either date is illegal,
%    0 otherwise.
%
  nday = 0;
  nhour = 0;
  nminute = 0;
  nsecond = 0;
  ierror = 0;
%
%  Compare the dates.
%
  cmp = ymdhms_compare ( y1, m1, d1, h1, n1, s1, y2, m2, d2, h2, n2, s2 );
%
%  We swap dates, if necessary, so that date 1 is never greater
%  than date 2.
%
  if ( cmp == '>' )
     [ y1, m1, d1, h1, n1, s1, y2, m2, d2, h2, n2, s2 ] = ymdhms_swap ( ...
       y1, m1, d1, h1, n1, s1, y2, m2, d2, h2, n2, s2 );
  end
%
%  Set beginning to 00:00:00 1 Jan Y1.
%    end       to 00:00:00 1 Jan Y1.
%
  nhour   = h2 - h1;
  nminute = n2 - n1;
  nsecond = s2 - s1;
  nday    = 0;
%
%  Subtract seconds, minutes, hours
%
  while ( nsecond < 0 )
    nsecond = nsecond + 60;
    nminute = nminute - 1;
  end

  while ( nminute < 0 )
    nminute = nminute + 60;
    nhour = nhour - 1;
  end

  while ( nhour < 0 )
    nhour = nhour + 24;
    nday = nday - 1;
  end
%
%  Set beginning to H1:M1:S1 1 Jan Y1
%    end          H2:M2:S2 1 Jan Y2.
%
  for y = y1 : y2 - 1
    nday = nday + year_length_days_common ( y );
  end
%
%  Set beginning to H1:M1:S1 1 Jan Y1
%    end          H2:M2:S2 1 M2  Y2.
%
  for m = 1 : m2 - 1
    nday = nday + month_length_common ( y2, m );
  end
%
%  Set beginning to H1:M1:S1 1  Jan Y1
%    end          H2:M2:S2 D2 M2  Y2.
%
  nday = nday + ( d2 - 1 );
%
%  Set beginning to H1:M1:S1 1  M1 Y1
%    end          H2:M2:S2 D2 M2 Y2.
%
  for m = 1 : m1 - 1
    nday = nday - month_length_common ( y1, m );
  end
%
%  Set beginning to H1:M1:S1 D1 M1 Y1
%    end          H2:M2:S2 D2 M2 Y2.
%
  nday = nday - ( d1 - 1 );
%
%  If we swapped dates, then the differences are
%  correct, but the signs should be negated.
%
%  Set beginning to H2:M2:S2 D2 M2 Y2,
%    end       to H1:M1:S1 D1 M1 Y1.
%
  if ( cmp == '>' )

    nday = - nday;
    nhour = - nhour;
    nminute = - nminute;
    nsecond = - nsecond;

     [ y1, m1, d1, h1, n1, s1, y2, m2, d2, h2, n2, s2 ] = ymdhms_swap ( ...
       y1, m1, d1, h1, n1, s1, y2, m2, d2, h2, n2, s2 );

  end

  return
end
