> Erlang中文手册 > time_difference/2 比较计算两个时间的差值

calendar:time_difference/2

比较计算两个时间的差值

用法:

time_difference(T1, T2) -> {Days, Time}

内部实现:

-spec time_difference(T1, T2) -> {Days, Time} when
      T1 :: datetime(),
      T2 :: datetime(),
      Days :: integer(),
      Time :: time().
time_difference({{Y1, Mo1, D1}, {H1, Mi1, S1}}, 
		{{Y2, Mo2, D2}, {H2, Mi2, S2}}) ->
    Secs = datetime_to_gregorian_seconds({{Y2, Mo2, D2}, {H2, Mi2, S2}}) -
	datetime_to_gregorian_seconds({{Y1, Mo1, D1}, {H1, Mi1, S1}}),
    seconds_to_daystime(Secs).

这个函数返回两个格式为 {Date, Time} 的元组时间差值。T2 是一个晚于 T1 的新纪元时间。

calendar:time_difference({{2013, 11, 15}, {17, 59, 33}}, {{2013, 11, 19}, {11, 23, 49}}).